[英]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
.....
其中“金額”是對象類型。 我想計算每個 ID 的總和和平均金額。 首先,我嘗試將“金額”列從對象轉換為浮點數
df['Amount'] = pd.to_numeric(df['Amount'], errors='coerce')
但是我在“金額”列中的所有值都得到了NaN
ID customer Month Amount
0 026 201707 NaN
....
如何將列對象類型轉換為帶有實數的浮點數並聚合每個客戶的值(總和、平均值、平均值)?
使用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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.