如何在没有索引的大 pandas 中转置数据框? [英] How do I transpose dataframe in pandas without index?

查看:43
本文介绍了如何在没有索引的大 pandas 中转置数据框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以肯定,这很简单.

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

I am reading a csv file and have the dataframe:

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时,会导致

However, when I do df.T, it results in

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

如何摆脱最上面的索引?

How do I get rid of the indexes on top?

推荐答案

您可以先将索引设置为数据框中的第一列(或通常要用作索引的列),然后转置该数据框.例如,如果要用作索引的列是'Attribute',则可以执行以下操作:

You can set the index to your first column (or in general, the column you want to use as as index) in your dataframe first, then transpose the dataframe. For example if the column you want to use as index is 'Attribute', you can do:

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

df.set_index('Attribute').T

这篇关于如何在没有索引的大 pandas 中转置数据框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆