繁体   English   中英

对于一列的所有唯一“单词”,从另一列中找到唯一的单元格,然后在python中平均另一列的对应值

[英]For all the unique 'words" of a column, the find unique cells from another column , then average corresponding values of another column in python

假设我有一个包含以下数据的 FILE.CSV

可乐 寒冷的
FCPP 2019年 5
FCPP 2020年 20
FCPP 2020年 10
PSGS 2020年 20
PSGS 2019年 30
PSGS 2019年 20

我想从 colA 中获取唯一名称,然后从 colB 中找出对应的唯一年份,然后对 colD 的值求平均值。 我的输出应该如下所示

可乐 寒冷的
FCPP 2019年 5
FCPP 2020年 15
PSGS 2019年 25
PSGS 2020年 20

我知道如何使用 df.unique() 在 Pandas 中找到唯一值,我可以使用

df.groupby(['colA'])['colD'].mean()

但不确定如何在命令中适应 colB。

简单groupby

out = df.groupby(['colA','colB'],as_index=False).mean()
Out[114]: 
   colA  colB  colD
0  FCPP  2019     5
1  FCPP  2020    15
2  PSGS  2019    25
3  PSGS  2020    20

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM