I have a dataframe input like this :
data = {'annee': [2015, 2015],
'medaille': ['Argent', 'Bronze'],
'categorie': ['Punchs', 'Punchs'],
'raisonSociale': ['Héritiers H Clément', 'Héritiers H Clément'],
'Marque': ['Clément', 'Clément'],
'Medaille_Rhum': [{'annee': 2015,
'medaille': 'Argent',
'produit': np.nan,
'Rhum_Name': 'PUNCH CAFE CLEMENT',
'Age': np.nan,
'labels': np.nan},
{'annee': 2015,
'medaille': 'Bronze',
'produit': np.nan,
'Rhum_Name': 'PUNCH PINA COLADA CLEMENT',
'Age': np.nan,
'labels': np.nan}],
'Or': [0, 0],
'Argent': [1, 0],
'Bronze': [0, 1],
'Score': [2, 1]}
df = pd.DataFrame(data)
Yes it's a dataframe of caribbean's rhums
I want to use pivot_table() to have 'annee' (e.g. year) in columns with score values. I achieve it, but I also want to have the 'Rhum_Name' column aggregate in a list of dict.
I tried the following code :
def ListDict(df):
listDict = []
listDict.extend(df)
return listDict
df_ranking = df_ranking.pivot_table(index=['raisonSociale', 'Marque', 'categorie'],
columns =['annee', 'Medaille_Rhum'],
values = 'Score',
fill_value = '',
aggfunc = {'annee': sum, 'Medaille_Rhum': ListDict},
margins = True,
margins_name = 'Total')
I get this error TypeError: unhashable type: 'dict'. I asume it's because, pivot_table wants to iterate directly over the dict, but I can't picture how to make it work.
Thanks in advance guys !