1
id
で最初にグループ化しようとしていて、それぞれid
をカウントし、何個の行がscore
>avg
であるかを数えます。Python Pandasグループの条件でフィルタを適用
データフレーム:
id col1 avg score
a 1 3 3
a 0 4 3
a 1 3 5
b 1 2 4
b 1 4 5
とします
id score>avg total
a 1 3
b 2 2
私のコード:私が得た
df2 = df.groupby('id', as_index=False)[['score'] > ['avg']].agg({'score>avg': 'count', 'total': 'count'})
エラー:
KeyError: 'Column not found: False'
[['score'] > ['avg']]
部分を編集する必要があるかどうかわかりません。