【发布时间】:2021-12-27 03:56:57
【问题描述】:
我有以下熊猫数据框:
df = pd.DataFrame({'Colors': ['blue', 'blue', 'orange', 'red',
'purple', 'orange', 'purple', 'blue', 'brown'],
'Price': ['500', '500', '200', '250', '300', '765', '1100', '762',
'650', '625'],
'Style': ['farm', 'contemporary', 'modern', 'MDM', 'MDM',
'contemporary', 'farm', 'contemporary', 'farm'],
'Location': ['far', 'near', 'far', 'far', 'near', 'far', 'far', 'near',
'far']})
我可以通过df.groupby(['Colors', 'Price', 'Style', 'Location']).size() 获取按颜色分类的信息,以查看价格、样式和位置的独特组合,以将颜色作为系列返回。
问题 - 我如何使用它来创建一个新的 pandas Dataframe,其中每列对应一种颜色(蓝色、橙色、红色等)和值的唯一组合(500_contemporary_near、500_farm_far 等)?
我尝试生成的输出示例是一个数据帧,其中有一列名为“Blue”,(每一行)下的每个值都是一个字符串,例如 500_contemporary_near。
蓝色
500_contemporary_near
500_farm_far
【问题讨论】:
标签: python pandas dataframe group-by