按行和列将 Pandas 数据框的元素重组为新数据框
Reorganize elements of Pandas Dataframe by row and column to new dataframe
我有以下数据框:
1 2 3 4 5 6
0 NaN NaN NaN a b c
1 NaN NaN NaN d e f
2 NaN NaN NaN g h i
0 1.0 2.0 3.0 -5.0 -4.0 -36.0
1 4.0 5.0 6.0 -32.0 -31.0 -120.0
2 7.0 8.0 9.0 102.0 126.0 3.0
这是四个数据帧的产物,因此每个季度(大小 3x3)也可作为独立的 pandas 数据帧使用。
我需要一个结构如下的数据框:
1 2 3 4 5 6
1 1 a 2 d 3 g
2 4 b 5 e 6 h
3 7 c 8 f 9 i
用文字描述:
so the first element of the third row is followed by the first element of the third Column.
second element of the third row ... second element of the third Col
third element of the third row ... third element of the third C..
so the first element of the fourth row is followed by the first element of the fourth Column.
second element of the fourth row ... second element of the fourth Col
third element of the fourth row ... third element of the fourth C..
so the first element of the fifth row is followed by the first element of the fifth Column.
second element of the fifth row ... second element of the fifth Col
third element of the fifth row ... third element of the fifth C..
欢迎和赞赏任何想法。请考虑:
- 6x6 是示例性数据帧,可能会有所不同,可能是 4x4 或大于 6x6
- 8x4 也有可能发生
更新:
好吧,也许可以使用 df.T(转置)右上角的数据帧(abc..hi)并通过一个循环来放置一列 df(较低的 lfet),然后是一列转置的右上角数据帧。因为我现在必须去上班,我明天会试一试并再次更新我的Post
根据我的理解,你可以使用 df 的形状并抓住左下和右上矩阵并将它们连接起来:
a,b = df.shape
m = int(a/2)
x = pd.DataFrame(df.iloc[:m,np.r_[m:b]].to_numpy()).T
y = pd.DataFrame(df.iloc[m:,:m].to_numpy())
out = (pd.concat((y,x),axis=1).sort_index(axis=1)
.set_axis(df.columns,axis=1,inplace=False))
print(out)
1 2 3 4 5 6
0 1.0 a 2.0 d 3.0 g
1 4.0 b 5.0 e 6.0 h
2 7.0 c 8.0 f 9.0 i
我有以下数据框:
1 2 3 4 5 6
0 NaN NaN NaN a b c
1 NaN NaN NaN d e f
2 NaN NaN NaN g h i
0 1.0 2.0 3.0 -5.0 -4.0 -36.0
1 4.0 5.0 6.0 -32.0 -31.0 -120.0
2 7.0 8.0 9.0 102.0 126.0 3.0
这是四个数据帧的产物,因此每个季度(大小 3x3)也可作为独立的 pandas 数据帧使用。 我需要一个结构如下的数据框:
1 2 3 4 5 6
1 1 a 2 d 3 g
2 4 b 5 e 6 h
3 7 c 8 f 9 i
用文字描述:
so the first element of the third row is followed by the first element of the third Column.
second element of the third row ... second element of the third Col
third element of the third row ... third element of the third C..
so the first element of the fourth row is followed by the first element of the fourth Column.
second element of the fourth row ... second element of the fourth Col
third element of the fourth row ... third element of the fourth C..
so the first element of the fifth row is followed by the first element of the fifth Column.
second element of the fifth row ... second element of the fifth Col
third element of the fifth row ... third element of the fifth C..
欢迎和赞赏任何想法。请考虑:
- 6x6 是示例性数据帧,可能会有所不同,可能是 4x4 或大于 6x6
- 8x4 也有可能发生
更新: 好吧,也许可以使用 df.T(转置)右上角的数据帧(abc..hi)并通过一个循环来放置一列 df(较低的 lfet),然后是一列转置的右上角数据帧。因为我现在必须去上班,我明天会试一试并再次更新我的Post
根据我的理解,你可以使用 df 的形状并抓住左下和右上矩阵并将它们连接起来:
a,b = df.shape
m = int(a/2)
x = pd.DataFrame(df.iloc[:m,np.r_[m:b]].to_numpy()).T
y = pd.DataFrame(df.iloc[m:,:m].to_numpy())
out = (pd.concat((y,x),axis=1).sort_index(axis=1)
.set_axis(df.columns,axis=1,inplace=False))
print(out)
1 2 3 4 5 6
0 1.0 a 2.0 d 3.0 g
1 4.0 b 5.0 e 6.0 h
2 7.0 c 8.0 f 9.0 i