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

查看:23
本文介绍了如何在没有索引的情况下转置 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`

如何去掉顶部的索引?

推荐答案

您可以先将索引设置为数据帧中的第一列(或者一般来说,您要用作索引的列),然后转置数据帧.例如,如果要用作索引的列是 '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天全站免登陆