将pandas数据帧转换为篮式橙色表

时间:2016-05-16 02:32:07

标签: python pandas orange

我有以下pandas数据框,并希望将其转换为带有篮子格式的Orange表格中的任何指针:

orders_item=pandas.DataFrame({'order_number':['001','002','002','002','003','003','003','003','003'],
'item':['Beef','Beef','Fish','Eggs','Beef','Oil','Salt','Pepper','MSG']})

我想把它转换成一个类似于这的篮子格式的橙色表:

Beef

Beef, Fish, Eggs

Beef, Oil, Salt, Pepper, MSG

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

我通过order_id分组并打印结果:

In [11]: for row in orders_item.groupby("order_number")["item"]:
             print(",".join(row[1]))
Beef
Beef,Fish,Eggs
Beef,Oil,Salt,Pepper,MSG

如果你想要字符串:

In [12]: "\n\n".join([", ".join(row[1]) for row in orders_item.groupby("order_number")["item"]])
Out[12]: 'Beef\n\nBeef, Fish, Eggs\n\nBeef, Oil, Salt, Pepper, MSG'