Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # We have df with three columns: "Country", "Medal" and "Count":
- new_df = df.pivot(index = "Country", columns = "Medal", values = "Count").fillna(0)
- # new_df has for the index "Contry" and three columns: "Bronze", "Silver" and "Gold" with values
- new_df = df.set_index(['Country', 'Medal']).unstack(fill_value = 0)
- # new_df has for the index "Contry" and three columns: "Bronze", "Silver" and "Gold" with values
- ################################################################################################
- # table2 has additional column "Year" and we want to unstack the column "Medal"
- # we can use only unstack()
- table2.groupby(["Country", "Year", "Medal"]).Count.sum().unstack().fillna(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement