python - simplify pandas dataframe after groupby operation -


i have foll dataframe result of groupby operation:

region  year   1.0     2015.0    6.775457e+05         2016.0    6.819761e+05         2017.0    6.864065e+05 

how can convert foll:

region  year      val 1.0     2015.0    6.775457e+05 1.0     2016.0    6.819761e+05 1.0     2017.0    6.864065e+05 

i tried using unstack() not work

you need reset_index convert series dataframe:

df = df.groupby(['region', 'year'])['val'].sum().reset_index(name='val') print (df)    region    year       val 0     1.0  2015.0  677545.7 1     1.0  2016.0  681976.1 2     1.0  2017.0  686406.5 

or if aggregate sum, mean add parameter as_index=false:

df = df.groupby(['region', 'year'], as_index=false)['val'].sum() print (df)    region    year       val 0     1.0  2015.0  677545.7 1     1.0  2016.0  681976.1 2     1.0  2017.0  686406.5 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -