我有一个具有以下结构的数据框:
Group Subgroup Results1 Results2
A s1 OK NOK
A s1 OK OK
A s2 NOK NOK
A s2 OK NOK
B s3 OK Not tested
B s3 Not tested NOK
B s4 OK NOK
我想以某种方式获取每个结果列的计数:
Results1 Results2
OK NOK Not tested OK NOK Not tested
A s1 2 1 0 1 2 0
A s2 1 3 ... ... ... ...
B s3 ... ... ... ... ... ...
B s4 ... ... ... ... ... ...
有什么办法可以用熊猫来做到这一点?
答案 0 :(得分:1)
将DataFrame.melt
与crosstab
和DataFrame.rename_axis
一起使用以删除列名称:
df = df.melt(['Group','Subgroup'])
df = (pd.crosstab([df['Group'], df['Subgroup']], [df['variable'], df['value']])
.rename_axis([None, None], axis=1))
print (df)
Results1 Results2
NOK Not tested OK NOK Not tested OK
Group Subgroup
A s1 0 0 2 1 0 1
s2 1 0 1 2 0 0
B s3 0 1 1 1 1 0
s4 0 0 1 1 0 0