答案 0 :(得分:2)
import pandas as pd
data = [['a', 'TRUE'], ['a', 'FALSE'], ['a', 'TRUE'], ['b', 'TRUE'], ['b', 'TRUE'], ['b', 'TRUE'],
['b', 'FALSE'], ['c', 'TRUE'], ['c', 'TRUE']]
df = pd.DataFrame(data, columns=['ID', 'PASS'])
df['value'] = 1
result = df.pivot_table(values='value', index='ID', columns='PASS', aggfunc='sum', fill_value=0)
result['Total'] = result.agg(sum, axis=1)
result
PASS FALSE TRUE Total
ID
a 1 2 3
b 1 3 4
c 0 2 2
答案 1 :(得分:0)
另一种方法是通过groupby和unstack,例如:
df = df.groupby(["ID","PASS"])['PASS'].count().unstack(fill_value=0)
df['total'] = df['FALSE']+df['TRUE']
所需结果:
PASS FALSE TRUE Total
ID
a 1 2 3
b 1 3 4
c 0 2 2