1
먼저 id
을 그룹화하고 각 id
을 계산합니다. 얼마나 많은 행이 score
>avg
인지 계산합니다.Python Pandas 그룹이 다음 조건으로 필터링합니다.
dataframe :
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']]
부분을 편집해야하는 이유가 확실하지 않습니다.