Pandas – flatten columns
Question:
After:
- aggregating with
sum()
- grouping by
['country', 'match_id']
- creating a mean column with
mean(axis=1)
I ended up with this:
Gls Ast avg_attack
sum sum
country match_id
Argentina 20eb96e2 0.10 0.20 0.15
18eb43e2 0.20 0.30 0.25
...
Now, how do I flatten my dataframe back to this?
country match_id Gls Ast avg_attack
Argentina 20eb96e2 0.10 0.20 0.15
Argentina 18eb43e2 0.20 0.30 0.25
Answers:
Use pandas.Index.get_level_values
to flatten the hierarchical index in columns then pandas.DataFrame.reset_index
to reset the multi index in rows.
df.columns = df.columns.get_level_values(0)
out = df.reset_index()
# Output :
print(out)
Country match_id Gls Ast avg_attack
0 Argentina 20eb96e2 0.10 0.20 0.15
1 Argentina 18eb43e2 0.20 0.30 0.25
After:
- aggregating with
sum()
- grouping by
['country', 'match_id']
- creating a mean column with
mean(axis=1)
I ended up with this:
Gls Ast avg_attack
sum sum
country match_id
Argentina 20eb96e2 0.10 0.20 0.15
18eb43e2 0.20 0.30 0.25
...
Now, how do I flatten my dataframe back to this?
country match_id Gls Ast avg_attack
Argentina 20eb96e2 0.10 0.20 0.15
Argentina 18eb43e2 0.20 0.30 0.25
Use pandas.Index.get_level_values
to flatten the hierarchical index in columns then pandas.DataFrame.reset_index
to reset the multi index in rows.
df.columns = df.columns.get_level_values(0)
out = df.reset_index()
# Output :
print(out)
Country match_id Gls Ast avg_attack
0 Argentina 20eb96e2 0.10 0.20 0.15
1 Argentina 18eb43e2 0.20 0.30 0.25