Pandas / Python如何在保持df结构的同时切换数据框中的索引/列? [英] Pandas/Python How to switch Index/Columns in dataframe while retaining df structure?
问题描述
我有一个如下所示的Pandas数据框:
I have a Pandas dataframe that looks like this:
X1 X1 X1 X2 X2 X2
ABC 12.4 34.3 25.4 29.3 53.2 38.9
DEF 22.3 28.6 32.8 24.6 29.4 25.3
左列是索引,最上面的值是列标签。我正在尝试交换列名和索引,以便看起来像这样:
The left column is the index, and the top values are column labels. I am trying to swap the column names and index so that it looks like this:
ABC ABC ABC DEF DEF DEF
X1 12.4 34.3 25.4 22.3 28.6 32.8
X2 29.3 53.2 38.9 24.6 29.4 25.3
我可以如果我添加了编号索引,则使用堆叠和拆栈来切换轴,但是重复项将垂直列出,而不是水平列出。我不知道该怎么做,以使各个副本并排放置,这对于我要对表格进行处理是必需的。重复项需要保持分开,我不希望平均值/总和/等。
I can get the axes switched using stack and unstack if I add a numbered index, but the replicates are listed vertically instead of horizontally. I can't figure out how to do it so that the individual replicates stay side-by-side, which is necessary for what I am trying to do with the table. The replicates need to stay separate, I do not want the Average/Sum/etc.
任何帮助/建议将不胜感激。
Any help/suggestions would be greatly appreciated.
谢谢!
编辑:
此代码给出的数据框的结构与我的实际数据相似,但具有较少的列:
This code gives a dataframe that is similar in structure to my actual data but with fewer columns:
names = ["G1","G2","G3","G4", "G5", "G6", "G7", "G8"]
df = pd.DataFrame([(7.345,"NaN","NaN",239.947,295.893,349.834),(13.872,"NaN","NaN",20.485,14.852,29.598),(764.298,"NaN","NaN",492.854,432.943,539.950),(0.00385,"NaN","NaN",0.184,0.384,0.285),(285.836,"NaN","NaN",495.284,395.486,368.952),(7.385,"NaN","NaN",5.293,4.295,4.692),(21.693,"NaN","NaN",25.843,15.843,15.386),(8.583,"NaN","NaN",4.397,6.295,6.39)], names, ["S1", "S1", "S1", "482.1", "482.1", "482.1"])
提供此数据框:
S1 S1 S1 482.1 482.1 482.1
G1 7.34500 NaN NaN 239.947 295.893 349.834
G2 13.87200 NaN NaN 20.485 14.852 29.598
G3 764.29800 NaN NaN 492.854 432.943 539.950
G4 0.00385 NaN NaN 0.184 0.384 0.285
G5 285.83600 NaN NaN 495.284 395.486 368.952
G6 7.38500 NaN NaN 5.293 4.295 4.692
G7 21.69300 NaN NaN 25.843 15.843 15.386
G8 8.58300 NaN NaN 4.397 6.295 6.390
运行:
df2 = df.copy()
m = dict(zip(df2.index.unique(), df2.columns.unique()))
df2.index = df2.index.map(m.get)
df2.columns = df2.columns.map({v : k for k, v in m.items()}.get)
给予:
G1 G1 G1 G2 G2 G2
S1 7.34500 NaN NaN 239.947 295.893 349.834
482.1 13.87200 NaN NaN 20.485 14.852 29.598
NaN 764.29800 NaN NaN 492.854 432.943 539.950
NaN 0.00385 NaN NaN 0.184 0.384 0.285
NaN 285.83600 NaN NaN 495.284 395.486 368.952
NaN 7.38500 NaN NaN 5.293 4.295 4.692
NaN 21.69300 NaN NaN 25.843 15.843 15.386
NaN 8.58300 NaN NaN 4.397 6.295 6.390
列和索引标签已移动,但是与它们关联的数据没有移动,并且缺少几列。运行:
The column and index labels have moved, but the data associated with them have not, and several columns are missing. Running:
df2 = df.copy()
m = dict(zip(df2.index.unique(), df2.columns.unique()))
df2 = df2.rename(index=m, columns={v : k for k, v in m.items()})
给予:
G1 G1 G1 G2 G2 G2
S1 7.34500 NaN NaN 239.947 295.893 349.834
482.1 13.87200 NaN NaN 20.485 14.852 29.598
G3 764.29800 NaN NaN 492.854 432.943 539.950
G4 0.00385 NaN NaN 0.184 0.384 0.285
G5 285.83600 NaN NaN 495.284 395.486 368.952
G6 7.38500 NaN NaN 5.293 4.295 4.692
G7 21.69300 NaN NaN 25.843 15.843 15.386
G8 8.58300 NaN NaN 4.397 6.295 6.390
出于类似原因,这也是错误的。
Which is also wrong for similar reasons.
推荐答案
New_df=df.T.groupby(level=0).agg(lambda x : x.values.tolist()).stack().apply(pd.Series).unstack().sort_index(level=1,axis=1)
New_df.columns=New_df.columns.droplevel(level=0)
New_df
Out[229]:
ABC ABC ABC DEF DEF DEF
X1 12.4 34.3 25.4 22.3 28.6 32.8
X2 29.3 53.2 38.9 24.6 29.4 25.3
这篇关于Pandas / Python如何在保持df结构的同时切换数据框中的索引/列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!