熊猫重新格式化表格

时间:2017-04-30 23:51:08

标签: python pandas

也许这是一个微不足道的问题,但我如何转换此数据框

Country_code    Country       2006 2007 2008 2009 
      1         Germany       12    15   23   22
      2         Italy         28    23   44   .. 

为:

Country_code    Country  Year  value
   1             Germany  2006   12
   1             Germany  2007   15
   1             Germany  2008   23
   1             Germany  2009   22
   2             Italy    2006   28
   2             Italy    2007   23
   2             Italy    2008   44
   2             Italy    2009   None

2 个答案:

答案 0 :(得分:2)

import pandas as pd
df = pd.DataFrame({'2006': {0: 12, 1: 28},
 '2007': {0: 15, 1: 23},
 '2008': {0: 23, 1: 44},
 '2009': {0: '22', 1: 'None'},
 'Country': {0: 'Germany', 1: 'Italy'},
 'Country_code': {0: 1, 1: 2}})
#use melt function to pivot the data.
pd.melt(df,id_vars=['Country_code','Country'],var_name='Year',value_name='Value').sort_values(by='Country')

   Country_code  Country  Year Value
0             1  Germany  2006    12
2             1  Germany  2007    15
4             1  Germany  2008    23
6             1  Germany  2009    22
1             2    Italy  2006    28
3             2    Italy  2007    23
5             2    Italy  2008    44
7             2    Italy  2009  None

答案 1 :(得分:1)

另一种解决方案是使用set_indexstack

df.set_index(['Country_code','Country']).stack().reset_index().rename(columns={'level_2':'Year',0:'value'}`)

输出:

   Country_code  Country  Year value
0             1  Germany  2006    12
1             1  Germany  2007    15
2             1  Germany  2008    23
3             1  Germany  2009    22
4             2    Italy  2006    28
5             2    Italy  2007    23
6             2    Italy  2008    44
7             2    Italy  2009  None