2016-11-17 78 views
-1

我有一个数据透视表,我想绘制每个城镇每年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 
+0

分享代码文本,而不是截图 – Boud

+0

请阅读[如何制作熊猫示例](http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples)和[mcve] (http://stackoverflow.com/help/mcve)。 –

回答

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 
+0

更新后我不清楚你是否真的想要一个数据帧/系列作为结果或情节,但这里都是... –