在 Pandas 中转置 DataFrame,同时保留索引列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32958399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 23:58:59  来源:igfitidea点击:

Transpose DataFrame in Pandas while preserving Index column

pandasindexingdataframetranspose

提问by O.rka

The problem is, when I transpose the DataFrame, the header of the transposed DataFrame becomes the Index numerical values and not the values in the "id" column. See below original data for examples:

问题是,当我转置 DataFrame 时,转置后的 DataFrame 的标题成为索引数值,而不是“id”列中的值。有关示例,请参见以下原始数据:

Original data that I wanted to transpose (but keep the 0,1,2,... Index intact and change "id" to "id2" in final transposed DataFrame).
enter image description hereDataFrame after I transpose, notice the headers are the Index values and NOT the "id" values (which is what I was expecting and needed)enter image description here

我想转置的原始数据(但保持 0,1,2,... 索引完整,并在最终转置的 DataFrame 中将“id”更改为“id2”)转置后的数据帧,请注意标题是索引值而不是“id”值(这是我所期望和需要的)
在此处输入图片说明在此处输入图片说明

Logic Flow

逻辑流程

First this helped to get rid of the numerical index that got placed as the header: How to stop Pandas adding time to column title after transposing a datetime index?

首先,这有助于摆脱作为标题放置的数字索引:如何在转置日期时间索引后停止 Pandas 向列标题添加时间?

Then this helped to get rid of the index numbers as the header, but now "id" and "index" got shuffled around: Reassigning index in pandas DataFrame& Reassigning index in pandas DataFrame

然后,这有助于摆脱索引号作为标题,但现在“id”和“指标”得到抛去:重新分配指数大Pandas数据帧在大Pandas数据帧重新分配指标

enter image description here

在此处输入图片说明

But now my id and index values got shuffled for some reason.

但是现在我的 id 和 index 值由于某种原因被打乱了。

How can I fix this so the columns are [id2,600mpe, au565...]?

我该如何解决这个问题,使列是 [id2,600mpe, au565...]?

How can I do this more efficiently?

我怎样才能更有效地做到这一点?

Here's my code:

这是我的代码:

DF = pd.read_table(data,sep="\t",index_col = [0]).transpose() #Add index_col = [0] to not have index values as own row during transposition
m, n = DF.shape
DF.reset_index(drop=False, inplace=True)
DF.head()

This didn't help much: Add indexed column to DataFrame with pandas

这没有多大帮助:使用Pandas将索引列添加到 DataFrame

回答by ako

If I understand your example, what seems to happen to you is that you transposetakes your actual index (the 0...n sequence as column headers. First, if you then want to preserve the numerical index, you can store that as id2.

如果我理解您的示例,那么您似乎发生的事情是您transpose将实际索引(0...n 序列作为列标题。首先,如果您想保留数字索引,则可以将其存储为id2.

DF['id2'] = DF.index

Now if you want idto be the column headers then you must set that as an index, overriding the default one:

现在,如果您想 id成为列标题,则必须将其设置为索引,覆盖默认索引:

DF.set_index('id',inplace=True)
DF.T

I don't have your data reproduced, but this should give you the values of idacross columns.

我没有复制您的数据,但这应该为您提供id跨列的值。