Python Column Generation with AVERAGEIF equivalent to Excel [duplicate]

三世轮回 提交于 2019-12-11 16:26:55

问题


I've poured over StackOverflow and just cant figure this one out. I a data tables with unique letter# combinations, and im trying to get the "averageif" of those values into the second table. Basically, I am trying to generate the "AVG" column below.

Letter  Value      Unique   Avg
A1  7.97           A1       5.76
A1  3.56           A2       5.34 
A2  3.16           A3       1.53
A2  9.09           B1       4.55
A2  0.64           B2       5.43
A2  8.48           B3       6.76
A3  1.53
B1  4.55
B2  7.36
B2  3.81
B2  7.30
B2  4.39
B2  4.30
B3  4.61
B3  8.91

I've used pd.DataFrame to isolate the unique values in the first data set as a DataFrame of unique values, but I'm really having trouble. I have all the code to pull this data in from a csv, generate the unique values, etc but it doesnt seem relevant. Thanks for any advice, I just cant get passed this step.

Best


回答1:


Well, you can do that using groupby as follows:

df.groupby('Letter').mean()


来源:https://stackoverflow.com/questions/47336754/python-column-generation-with-averageif-equivalent-to-excel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!