My question is pretty simple but I haven't found an answer for it anywhere on stackexchange or google. In pandas for python, how do you convert a series with two columns back into a dataframe? My series (agggenfreq) is below. The code that generated it was this:
agggenfreq=df2000[['freq','name','sex']].groupby(['name', 'sex']).sum()['freq']
Where df2000 is a dataframe containing the following columns:
name sex freq year agg
0 Hannah F 23071 2000 162834
1 Hannah M 25 2000 162834
2 Hannah F 20706 2001 162834
3 Hannah M 22 2001 162834
4 Hannah F 18814 2002 162834
agggenfreq:
name sex
Aaden F 5
M 2980
Aaliah F 518
Aaliya F 583
Aaliyah F 40975
M 49
Aamir M 607
Aaniyah F 741
Aanya F 954
Aaralyn F 581
Aarav M 1374
Aaron F 365
M 94269
Aarush M 763
Aarya F 375
M 190
Aaryan M 560
Aaryn F 293
M 272
Aayush M 632
Abagail F 3051
Abbey F 5230
M 6
Is there a good way to convert this back into a dataframe? If there was only one column (which I think technically isn't the correct term but you should know what I mean) in the series here I would do it like this:
data = pd.DataFrame({'agg': aggfreq,'name':aggfreq.index})
How do I do this with two columns? Thanks in advance. EDIT: Also if someone knows of a better way to do the abovementioned (first) function in-line without having to make a series I would love to know :)
That is a Series with a MultiIndex. You can get a DataFrame by doing agggenfreq.reset_index().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With