如何以某种方式转置 pandas 数据框? [英] How do I transpose a pandas dataframe in a certain fashion?
本文介绍了如何以某种方式转置 pandas 数据框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给出如下数据框 df
:
Col1 Col2
Key
A 4 10
B 7 10
C 3 9
我想要的数据帧是
A B C
Col1 4 7 3
Col2 10 10 9
其中 Col1
和 Col2
是索引。
我该如何指定?我尝试过:
How would I specify this? I've tried:
In [419]: mydf.T.reset_index(drop=True)
Out[419]:
Key A B C
0 4 7 3
1 10 10 9
但是由于某些原因,密钥
仍然存在。我不确定它是什么,也不确定如何摆脱它。我也尝试过 mydf.T.reset_index()。set_index('index')
,但这非常难看。
But for some reason, the Key
remains. I'm not sure what it is, and I'm not sure how to get rid of it. I've also tried mydf.T.reset_index().set_index('index')
but it is very unsightly.
推荐答案
我们可以使用 DataFrame.rename_axis()此处:
In [24]: df.T.rename_axis(None, axis=1)
Out[24]:
A B C
Col1 4 7 3
Col2 10 10 9
这篇关于如何以某种方式转置 pandas 数据框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文