gpt4 book ai didi

python - 如何在没有索引的 Pandas 中转置数据框?

转载 作者:IT老高 更新时间:2023-10-28 22:21:57 26 4
gpt4 key购买 nike

很确定这很简单。

我正在读取一个 csv 文件并拥有数据框:

Attribute    A   B   C
a 1 4 7
b 2 5 8
c 3 6 9

我想做一个转置得到

Attribute    a   b   c
A 1 2 3
B 4 5 6
C 7 8 9

但是,当我执行 df.T 时,会导致

             0   1   2 
Attribute a b c
A 1 2 3
B 4 5 6
C 7 8 9`

如何去掉顶部的索引?

最佳答案

您可以先将索引设置为数据框中的第一列(或者通常是您要用作索引的列),然后转置数据框。例如,如果您要用作索引的列是 'Attribute',您可以这样做:

df.set_index('Attribute',inplace=True)
df.transpose()

或者

df.set_index('Attribute').T

关于python - 如何在没有索引的 Pandas 中转置数据框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42381639/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com