如何绘制数据透视表值?

时间:2016-11-17 15:08:07

标签: python pandas indexing dataframe pivot

我有一个数据透视表,我想绘制每个城镇每年12个月的价值。

                        2010-01        2010-02     2010-03 
City        RegionName      

Atlanta     Downtown    NaN         NaN          NaN
            Midtown     194.263702  196.319964   197.946962

Alexandria  Alexandria  NaN         NaN          NaN 
            West    
            Landmark-   NaN         NaN          NaN
            Van Dom 

如何只选择每个城镇的每个区域的值?我想也许最好将列名称用年份和月份更改为datetime格式并将其设置为index。我怎么能这样做?

enter image description here

结果必须是:

City        RegionName

2010-01    Atlanta     Downtown    NaN         
                       Midtown     194.263702  

           Alexandria  Alexandria  NaN          
                       West    
                       Landmark-   NaN         
                       Van Dom 

1 个答案:

答案 0 :(得分:1)

这里有一些类似的虚拟数据:

idx = pd.MultiIndex.from_arrays([['A','A', 'B','C','C'],
                            ['A1','A2','B1','C1','C2']], names=['City','Region'])
idcol = pd.date_range('2012-01', freq='M', periods=12)
df = pd.DataFrame(np.random.rand(5,12), index=idx, columns=[t.strftime('%Y-%m') for t in idcol])

让我们看看我们得到了什么:

print(df.ix[:,:3])

              2012-01   2012-02   2012-03
City Region                              
A    A1      0.513709  0.941354  0.133290
     A2      0.734199  0.005218  0.068914
B    B1      0.043178  0.124049  0.603469
C    C1      0.721248  0.483388  0.044008
     C2      0.784137  0.864326  0.450250

让我们将这些转换为日期时间:df.columns = pd.to_datetime(df.columns)

现在要绘制你只需要转置:

df.T.plot()

enter image description here

更新您的问题后更新:

使用堆栈,然后根据需要重新排序:

df = df.stack().reorder_levels([2,0,1])
df.head()


            City  Region
2012-01-01  A     A1        0.513709
2012-02-01  A     A1        0.941354
2012-03-01  A     A1        0.133290
2012-04-01  A     A1        0.324518
2012-05-01  A     A1        0.554125