Color Shape Value
red triangle 10
red circle 11
blue triangle 12
blue circle 13
我需要将其转换为矩阵形式的新数据框,其中列是颜色,索引是形状
red blue
triangle 10 12
circle 11 13
我通过循环迭代的方式做到了这一点
new_df = pd.DataFrame(columns=list_of_colors, index=list_of_shapes)
for color_i in list_of_colors:
# this gives me the values of each color sorted* by Shape
df[df['Color'] == color_i].sort_values('Shape')['Value']
# so I can append this to the new dataframe
...