问题描述
我确实具有以下数据框:
i do have the following 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)也可以作为独立的熊猫数据框使用.我需要一个像这样的数据框:
This is a product of four Dataframes, so each quarter (size 3x3) is also available as a isolated pandas dataframe.I need a Dataframe structured like this:
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..
任何想法都值得欢迎和赞赏.请考虑:
Any Idea is welcome and aprecciated. Please consider:
- 6x6是示例性数据帧,但可能相差4x4或大于6x6
- 可能还会出现8x4
更新:好的,也许可以使用df.T(转置)数据帧右上角(abc..hi)并经过一个循环,该循环将一列df(低端lfet)放入一列转置的右上数据帧中.由于我现在必须去上班,所以我明天尝试一下,然后再次更新我的帖子
UPDATE:Okay maybe its possible to use df.T (transpose) Dataframe upper right ( abc..hi) and go by a loop which puts one column of df (lower lfet) and then one column of transposed upper right dataframe. As i have to leave for work now I'll give it a try tomorrow and update my Post again
推荐答案
根据我的理解,您可以使用df的形状并抓住左下和右上矩阵并进行合并:
based on what i understand, you can use the shape of the df and grab the left bottom and right upper matrix and concat them:
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
这篇关于按行和列将Pandas Dataframe的元素重组为新的Dataframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!