【发布时间】:2014-02-25 08:37:27
【问题描述】:
我正在尝试过滤掉 groupby 的结果。
我有这张桌子:
A B C
A0 B0 0.5
A1 B0 0.2
A2 B1 0.6
A3 B1 0.4
A4 B2 1.0
A5 B2 1.2
A是索引,是唯一的。
其次,我有这个清单:
['A0', 'A1', 'A4']
我想按B 分组,并为每个组提取最高值为C 的行。必须在每个组中的所有行之间选择此行,为上面列表中存在索引的行提供最高优先级。
此数据和代码的结果必须是:
A B C
A0 B0 0.5
A2 B1 0.6
A4 B2 1.0
我认为的伪代码必须是:
group by B
for each group G:
intersect group G rows index with indexes in the list
if intersection is not void:
the group G becomes the intersection
sort the rows by C in ascending order
take the first row as representative for this group
如何在 pandas 中做到这一点?
谢谢
【问题讨论】:
标签: python filter group-by pandas