在python / pandas中调整列 [英] Transpose columns in python/pandas

查看:241
本文介绍了在python / pandas中调整列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 数据列(共7列):
Nane 3966非空值
值1 3966非空值
值2 3966非空值
值3 3966非空值
值4 3966非空值
Value5 3966非空值
期间3966非空值

我想要期限作为列,其他的作为行。



所以

 名称值1 ....价值5期间成为

期间1期2期3年期3966
名称
价值1
...
价值5

我已经尝试使用堆栈/拆包和转置功能,但我只是不能弄清楚。有没有人有任何指针?

解决方案

你想使用 Period 作为指标。 set_index 将为您做到这一点。因此,您可以转换生成的表:

  df.set_index('Period')。T 
输出[13] :
期间2010 2011 2012
名称foo bar nil
Value1 0,994369885 0,92930566 0,997754262
Value2 0,780942307 0,274566936 0,488064461
Value3 0,510782105 0,390724018 0,642086396
Value4 0,842522334 0,613705323 0,028703768
Value5 0,383279727 0,287280101 0,764773601


I'm trying to rearrange a dataframe with 5 variables

Data columns (total 7 columns):
Nane      3966  non-null values
Value1    3966  non-null values
Value2    3966  non-null values
Value3    3966  non-null values
Value4    3966  non-null values
Value5    3966  non-null values
Period    3966  non-null values

I'd like period to be the columns, and the other ones as rows.

So

Name Value1 .... Value 5 Period becomes

Period 1 period 2 period3 .... period 3966   
Name  
Value 1  
...  
Value 5

I've tried using the stack/unstack and the transpose function, but I just can't figure it out. Does anyone have any pointers ?

解决方案

You want to use the Period as an index. set_index will do this for you. THen you can transpose your resulting table:

df.set_index('Period').T
Out[13]: 
Period         2010         2011         2012
Name            foo          bar          nil
Value1  0,994369885   0,92930566  0,997754262
Value2  0,780942307  0,274566936  0,488064461
Value3  0,510782105  0,390724018  0,642086396
Value4  0,842522334  0,613705323  0,028703768
Value5  0,383279727  0,287280101  0,764773601

这篇关于在python / pandas中调整列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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