Используйте Series.str.replace
перед pd.to_numeric
для преобразования ,
в .
Тогда вы можете использовать groupby.agg
agg_df = (df.assign(Amount = pd.to_numeric(df['Amount'].str.replace(',','.'),
errors = 'coerce'))
.groupby('ID').Amount.agg(['mean','sum']))
print(agg_df)
#if you want change the type of Amount previously
#df['Amount'] =pd.to_numeric(df['Amount'].str.replace(',','.'),errors = 'coerce')
#agg_df = df.groupby('ID').Amount.agg(['mean','sum']))
mean sum
ID
0 31.65 31.65
1 31.65 31.65
2 31.65 31.65
3 31.65 31.65
4 31.65 31.65
Если вы хотите агрегировать в начальный фрейм данных GroupBy.transform
:
groups = pd.to_numeric(df['Amount'].str.replace(',','.'),errors = 'coerce').groupby(df['ID'])
#if you want change the type of Amount previously
#df['Amount'] =pd.to_numeric(df['Amount'].str.replace(',','.'),errors = 'coerce')
#groups = df.groupby('ID')['Amount']
df['mean'] = groups.transform('mean')
df['sum'] = groups.transform('sum')
print(df)
ID customer Month Amount mean sum
0 0 26 201707 31,65 31.65 31.65
1 1 26 201708 31,65 31.65 31.65
2 2 26 201709 31,65 31.65 31.65
3 3 26 201710 31,65 31.65 31.65
4 4 26 201711 31,65 31.65 31.65