我有下面的.csv
Name Location Product Type number
Greg 1 Fruit grape 1
Greg 1 Fruit apple 2
Greg 1 Bakery bread 5
Greg 1 Bakery roll 8
Greg 2 Fruit grape 7
Greg 2 Fruit apple 1
Greg 3 Fruit grape 2
Greg 4 Bakery roll 3
Greg 4 Bakery bread 4
Sam 5 Fruit apple 7
Sam 5 Fruit grape 9
Sam 5 Fruit apple 10
Sam 6 Bakery roll 11
Sam 6 Bakery bread 12
Sam 7 Fruit orange 13
Sam 7 Bakery roll 14
Tim 8 Fruit bread 16
Zack 9 Bakery roll 17
Zack 10 Fruit apple 19
Zack 10 Fruit grape 20
我想按名称将其分为熊猫和大熊猫组,在多个地点有两种以上的产品。我仍然希望保留产品的“编号”
例如,位置1的Greg有两个产品
name location product type
Greg 1 Fruit, bakery grape,apple,bread,roll
我正在与groupby进行斗争,并最终将其恢复到一个我可以使用的数据框架中
如果执行
df.groupby([col_names])
,列名称将成为索引为了将索引转换回列,需要使用
DataFrame.reset_index()
方法希望有帮助
IIUC将
transform
与nunique
一起使用相关问题 更多 >
编程相关推荐