python - How can I plot a pivot table value? -
i have pivot table , want plot values 12 months of each year each town.
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
how can select values each region of each town? thought maybe better change column names years , months datetime
format , set them index
. how can this?
the result must be:
city regionname 2010-01 atlanta downtown nan midtown 194.263702 alexandria alexandria nan west landmark- nan van dom
here's similar dummy data play with:
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') t in idcol])
let's see we've got:
print(df.ix[:,:3]) 2012-01 2012-02 2012-03 city region 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
let's convert these datetime: df.columns = pd.to_datetime(df.columns)
now plot need transpose:
df.t.plot()
update after updated question:
use stack, , reorder if want:
df = df.stack().reorder_levels([2,0,1]) df.head() city region 2012-01-01 a1 0.513709 2012-02-01 a1 0.941354 2012-03-01 a1 0.133290 2012-04-01 a1 0.324518 2012-05-01 a1 0.554125
Comments
Post a Comment