data=[
{"date":"1/2" , "Full name":"Yamada" ,"dessert" : "Apple" ,"Side dishes" :"Gyoza" },
{"date":"1/3" , "Full name":"Yamada" ,"dessert" : "Apple" ,"Side dishes" :"Gyoza" },
{"date":"1/4" , "Full name":"Yamada" ,"dessert" : "Banana" ,"Side dishes" :"Boiled fish" },
{"date":"1/5" , "Full name":"Yamada" ,"dessert" : "Apple" ,"Side dishes" :"Gyoza" },
{"date":"1/2" , "Full name":"Sasaki" ,"dessert" : "Orange" ,"Side dishes" :"Gyoza" },
{"date":"1/3" , "Full name":"Sasaki" ,"dessert" : "Apple" ,"Side dishes" :"Gyoza" },
{"date":"1/4" , "Full name":"Sasaki" ,"dessert" : "Apple" ,"Side dishes" :"Gyoza" },
{"date":"1/5" , "Full name":"Sasaki" ,"dessert" : "Apple" ,"Side dishes" :"stew" },
]
import pandas as pd
df=pd.DataFrame(data)
df
Date Name Dessert Side dish
0 1/2 Yamada apple dumplings
1 1/3 Yamada apple dumplings
2 1/4 Yamada Banana Boiled fish
3 1/5 Yamada apple dumplings
4 1/2 Sasaki orange dumplings
5 1/3 Sasaki apple dumplings
6 1/4 Sasaki apple dumplings
7 1/5 Sasaki apple stew
df.pivot_table(values= 'Side dishes', index='Full name', columns='dessert' , aggfunc="count" , fill_value=0)
values = If you do not enter'side dishes', it will be aggregated for each remaining column Number of cases with aggfunc = "count" If fill_value = 0 is not entered, 0 will be NaN.
Dessert Banana Apple Mandarin
Full name
Sasaki 0 3 1
Yamada 1 3 0
reference note.nkmk.me pandas.pivot_table
Recommended Posts