我有一个熊猫数据帧。我需要把一些列转换成行。数据帧的前两列中每3行有相同的数据。所以,我还需要6列,正如您在预期的数据帧中看到的那样。 我有以下数据帧:
shopCode Product Code Score
111 Apple 123 0.70
111 Apple 456 0.75
111 Apple 789 0.80
222 Orange 142 0.66
222 Orange 136 0.83
222 Orange 623 0.76
我期望的数据帧是:
^{pr2}$我试着用
df.pivot(index=['shopCode', 'Product'], columns=['Code1', 'Code2', 'Code3', 'Score1', 'Score2', 'Score3'], values=['Code', 'Score'])
但它不起作用。在
可以通过pandas groupby和字典合并实现。在
我们使用
pivot_table
在
^{pr2}$reset_index
之后相关问题 更多 >
编程相关推荐