この構造のデータフレームがあります。
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 ... ... ... ... ... ...
パンダでこれを達成する方法はありますか?
使用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
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加