O.r*_*rka 4 indexing transpose dataframe pandas
问题是,当我转置DataFrame时,转置的DataFrame的标头变为Index数值,而不是“ id”列中的值。有关示例,请参见以下原始数据:
我想转置的原始数据(但保持0,1,2,...索引不变,并在最终转置的DataFrame中将“ id”更改为“ id2”)。
我转置后的DataFrame,请注意标题是Index值,而不是“ id”值(这是我期望和需要的)

逻辑流程
首先,这有助于摆脱作为标题放置的数字索引:如何在转换日期时间索引后停止Pandas向列标题添加时间?
然后,这有助于摆脱索引号作为标头的作用,但现在“ id”和“ index”已被改组:在pandas DataFrame中重新分配索引&pandas DataFrame中重新分配索引
但是现在我的id和index值由于某种原因而被改组。
如何解决此问题,使列为[id2,600mpe,au565 ...]?
我怎样才能更有效地做到这一点?
这是我的代码:
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()
Run Code Online (Sandbox Code Playgroud)
这并没有太大帮助:使用pandas将索引列添加到DataFrame中
如果我理解您的示例,似乎会发生什么,您transpose将实际索引(0 ... n序列用作列标题)。首先,如果要保留数字索引,则可以将其存储为id2。
DF['id2'] = DF.index
Run Code Online (Sandbox Code Playgroud)
现在,如果要 id成为列标题,则必须将其设置为索引,以覆盖默认索引:
DF.set_index('id',inplace=True)
DF.T
Run Code Online (Sandbox Code Playgroud)
我没有复制您的数据,但这应该为您提供id跨列的值。
| 归档时间: |
|
| 查看次数: |
15006 次 |
| 最近记录: |