I need some help with Pandas.
I have following dataframe:
df = pd.DataFrame({'1Country': ['FR', 'FR', 'GER','GER','IT','IT', 'FR','GER','IT'],
'2City': ['Paris', 'Paris', 'Berlin', 'Berlin', 'Rome', 'Rome','Paris','Berlin','Rome'],
'F1': ['A', 'B', 'C', 'B', 'B', 'C', 'A', 'B', 'C'],
'F2': ['B', 'C', 'A', 'A', 'B', 'C', 'A', 'B', 'C'],
'F3': ['C', 'A', 'B', 'C', 'C', 'C', 'A', 'B', 'C']})
I am trying to do a groupby
on first two columns 1Country
and 2City
and do value_counts
on columns F1
and F2
. So far I was only able to do groupby and value_counts
on 1 column at a time with
df.groupby(['1Country','2City'])['F1'].apply(pd.Series.value_counts)
How can I do value_counts
on multiple columns and get a datframe as a result?