如何将列类型对象转换为数据框中的浮点数

how to convert columnt type object into float in dataframe

我有以下数据框:

        ID customer Month   Amount
    0   026         201707  31,65
    1   026         201708  31,65
    2   026         201709  31,65
    3   026         201710  31,65
    4   026         201711  31,65

.....

其中 'Amount' 是 object 类型。我想计算每个 ID 的 sumaverage 金额。 首先,我尝试将 'Amount' 列从 object 转换为 float

df['Amount'] = pd.to_numeric(df['Amount'], 错误='coerce')

但是对于 'Amount' 列

中的所有值,我得到 NaN
        ID customer Month   Amount
    0   026         201707  NaN 
    ....

如何将列对象类型转换为带有实数的浮点数并聚合每个客户的值(总和、平均值、平均值)?

pd.to_numeric之前使用Series.str.replace,转换为.然后就可以使用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