I have a dictionary, created from two columns in a dataframe using:
df_dict=df.groupby('column1')['column2'].agg(list).to_dict()
I then used Counter to count how many items were put into each key as values:
df_dict_counts = Counter(df_dict)
This gives me per key, how many items are present as the key's values, which is great.
But now, I want to count the frequency of the items per key and to print the count.
So my dictionary looks like this:
df_dict =
{
'Apples': ['big', 'medium', 'medium', 'medium' 'big','small'],
'Oranges': ['big', 'medium', 'big'],
'Bananas':['small', ''small', 'small','small', 'big'],
'Pineapples':['small', 'big', 'big','big']
}
and the output I am aiming for is something like this:
df_dict_counts =
{
'Apples': {'big':2, 'medium':3, 'small':1},
'Oranges': {'big':2, 'medium':1},
'Bananas': {'small':4, 'big':1},
'Pineapples': {'small':1, 'big':3}
}
if you could help me to then print the 'df_dict_counts' into a .csv file, it would be great!
Thanks!!