![](/img/trans.png)
[英]Grouping unique column values to sum of each unique value in pandas dataframe column
[英]Grouping unique column values to get average of each unique value in pandas dataframe column
我正在嘗試獲取 pandas dataframe 並從另一列中的相應值中獲取列中每個唯一值的平均值。
dataframe 看起來像這樣:
Charge Code Days
1001-000 132
1001-000 48
1001-000 12
1001-000 22
1001-000 38
1001-000 22
1001-000 36
1001-000 931
1001-000 973
1001-000 53
1001-000 69
1001-000 69
1001-000 973
1001-000 69
1001-000 69
1001-000 69
1001-000 52
1001-000 973
1001-000 87
1001-000 973
1001-000 55
1001-000 55
1001-000 55
1001-000 55
1001-000 220
1002-000 39
1002-000 28
1002-000 16
1003-000 945
1003-000 25
1003-000 41
我正在使用以下行:
df_Paid.groupby(level=0)['Charge Code'].mean()
嘗試獲得所需的 dataframe:
Charge Code Days
1001-000 244
1002-000 28
1003-000 337
這是每個收費代碼的平均天數。 當我使用此行時,出現以下錯誤:
DataError: No numeric types to aggregate
我不確定我做錯了什么。
使用df_Paid.groupby('Charge Code')['days'].mean()
你應該很好。 Here is a good reference for the use of groupby(): https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.groupby.html Look up Hierarchical Indexes there.
試試這個,也許——
df_Paid['Days'] = df_Paid['Days'].astype(int)
df_Paid.groupby(['Charge Code'])['Days'].mean()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.