我已经将CSV文件的2列分组了
(r3 = df.groupby(['Predicted_Label', 'Actual_Label']).size().unstack(fill_value=0)
print (r3))
得到以下结果:
“实际标签”的第5列和第6列应合并到“预测标签”的第1行中。 输出应采用以下形式: 如何使用Pandas创建上述新列?
答案 0 :(得分:1)
将assign
用于由numpy.diagonal
创建的新列,并将每行每列减去sum
,reindex
用于相同的列索引,例如索引值:< / p>
#strip traling whitespaces
df['Predicted_Label'] = df['Predicted_Label'].str.strip()
r3 = df.groupby(['Predicted_Label', 'Actual_Label']).size().unstack(fill_value=0)
a = np.diagonal(r3.reindex(columns=r3.index))
b = r3.sum(axis=1) - a
c = r3.sum().reindex(r3.index) - a
out = r3.assign(T1=a,T2=b,T3=c)
print (out)
0 1 2 3 5 6 T1 T2 T3
0 14 0 3 22 0 0 14 25 14
1 5 56 14 157 19 11 56 206 8
2 2 0 37 26 0 0 37 28 17
3 7 8 0 1805 0 1 1805 16 205