我有这样的数据框:
产品 | 颜色 | 等级 | 价格 | 尺寸 |
---|---|---|---|---|
苹果 | 黄色 | A | 1000 | 3 |
苹果 | 绿色 | B | 500 | 4 |
香蕉 | 黄色 | B | 2000 | 5 |
香蕉 | 绿色 | A | 3000 | 6 |
我想将数据框转换为:
产品 | 价格_黄色_A | 尺寸_黄色_A | 价格_黄色_B | 尺寸_黄色_B | 价格_绿色_A | 尺寸_绿色_A | 价格_绿色_B | 尺寸_绿色_B |
---|---|---|---|---|---|---|---|---|
苹果 | 1000 | 3 | 0 | 0 | 0 | 0 | 500 | 4 |
香蕉 | 0 | 0 | 2000 | 5 | 3000 | 6 | 0 | 0 |
如何做到这一点?
您可以使用
[pd.pivot()][1]
以及其他一些中间步骤来实现此目的。
首先创建一个组合的
Colour
和 Grade
的列,如下所示:
df['Combined'] = df['Colour'] + '_' + df['Grade']
0 Yellow_A
1 Green_B
2 Yellow_B
3 Green_A
Name: Combined, dtype: object
然后
pivot()
数据框,保留 Product
作为索引,使用新的 Combined
列作为列,使用 Price
和 Size
列作为值。 我添加了 .fillna(0)
以在空格中添加 0。像这样:
new_df = df.pivot(index='Product', columns=['Combined'], values=['Price', 'Size']).fillna(0)
Price Size
Combined Green_A Green_B Yellow_A Yellow_B Green_A Green_B Yellow_A Yellow_B
Product
Apple 0.0 500.0 1000.0 0.0 0.0 4.0 3.0 0.0
Banana 3000.0 0.0 0.0 2000.0 6.0 0.0 0.0 5.0
这将返回所需的表,但具有多级列,因此我们可以使用一些列表理解将列重命名为所需的列名称,如下所示:
new_df.columns = [col[0]+'_'+col[1] for col in new_df.columns]
Price_Green_A Price_Green_B Price_Yellow_A Price_Yellow_B Size_Green_A Size_Green_B Size_Yellow_A Size_Yellow_B
Product
Apple 0.0 500.0 1000.0 0.0 0.0 4.0 3.0 0.0
Banana 3000.0 0.0 0.0 2000.0 6.0 0.0 0.0 5.0
完整代码:
df['Combined'] = df['Colour'] + '_' + df['Grade']
new_df = df.pivot(index='Product', columns=['Combined'], values=['Price', 'Size']).fillna(0)
new_df.columns = [col[0]+'_'+col[1] for col in new_df.columns]