[英]How do i transpose a pandas Dataframe in a way, in which I am only gonna need two columns and one column is for the headline
I have a Dataframe which looks something along the lines of this:我有一个数据框,它看起来像这样:
A B C乙丙
1 Data1 Data2 Data3 1 数据1 数据2 数据3
2 Data3 Data1 Nan 2 数据3 数据1 南
3 Nan Data5 Nan 3 楠数据5 楠
And basically i want to get a Dataframe which looks like that:基本上我想得到一个看起来像这样的数据框:
1 1
A Data1一个数据1
A Data3一个数据3
B Data2 B数据2
B Data1 B数据1
B Data5 B数据5
C Data3 C数据3
I already found out that I am probably going to need the transpose function.我已经发现我可能需要转置功能。 But I don't know how to go on from there.但我不知道如何从那里继续。 I would be glad if you could help me out with that or if you find another way which solves this problem.如果您能帮我解决这个问题,或者您找到解决此问题的另一种方法,我会很高兴。
You can do a bit of function chaining:你可以做一些函数链接:
df = df.stack().reset_index(level=0, drop=True).sort_index().dropna()
df.stack()
aggregates them in one column but adds a multilevel index so we drop with .reset_index(level=0, drop=True)
then we sort the index so it orders the text with sort_index()
then finally we dropna()
. df.stack()
将它们聚合在一列中,但添加了一个多级索引,因此我们使用.reset_index(level=0, drop=True)
然后我们对索引进行排序,以便它使用sort_index()
对文本进行排序,最后我们dropna()
.
Outputs:输出:
A Data1
A Data3
B Data2
B Data1
B Data5
C Data3
If you take off each chained assignment and run them individually you can see what each intermediate step is doing: df.stack()
then df.stack().reset_index(level=0, drop=True)
, then df.stack().reset_index(level=0, drop=True).sort_index()
如果您取消每个链式分配并单独运行它们,您可以看到每个中间步骤在做什么: df.stack()
然后df.stack().reset_index(level=0, drop=True)
,然后df.stack().reset_index(level=0, drop=True).sort_index()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.