熊猫的属性合并和计数

时间:2018-08-26 04:23:17

标签: pandas python-3.6

我已经将CSV文件的2列分组了 (r3 = df.groupby(['Predicted_Label', 'Actual_Label']).size().unstack(fill_value=0) print (r3)) 得到以下结果:
Groupby count of dataframe

“实际标签”的第5列和第6列应合并到“预测标签”的第1行中。 输出应采用以下形式: enter image description here 如何使用Pandas创建上述新列?

1 个答案:

答案 0 :(得分:1)

assign用于由numpy.diagonal创建的新列,并将每行每列减去sumreindex用于相同的列索引,例如索引值:< / 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