如何在没有索引的情况下在pandas中转置数据帧?

use*_*511 28 python dataframe pandas

很确定这很简单.

我正在阅读一个csv文件并拥有数据帧:

Attribute    A   B   C
a            1   4   7
b            2   5   8
c            3   6   9
Run Code Online (Sandbox Code Playgroud)

我想做一个转置来获得

Attribute    a   b   c
A            1   2   3
B            4   5   6
C            7   8   9
Run Code Online (Sandbox Code Playgroud)

但是,当我做df.T时,它会导致

             0   1   2 
Attribute    a   b   c
A            1   2   3
B            4   5   6
C            7   8   9`
Run Code Online (Sandbox Code Playgroud)

如何摆脱顶部的索引?

dim*_*ab0 33

您可以先将索引设置为数据框中的第一列,然后进行转置吗?

df.set_index('Attribute',inplace=True)
df.transpose()
Run Code Online (Sandbox Code Playgroud)

要么

df.set_index('Attribute').T
Run Code Online (Sandbox Code Playgroud)


Tom*_*nch 8

这个对我有用:

>>> data = {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}
>>> df = pd.DataFrame(data, index=['a', 'b', 'c'])
>>> df.T
   a  b  c
A  1  2  3
B  4  5  6
C  7  8  9
Run Code Online (Sandbox Code Playgroud)