将 pandas 数据框转换为篮子类型橙色 table

Converting pandas dataframe into basket type orange table

我有以下 pandas 数据框,想要将其转换为具有篮子格式的橙色 table 的任何指示:

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']})

我想将其转换为橙色 table,篮子格式类似于:

Beef

Beef, Fish, Eggs

Beef, Oil, Salt, Pepper, MSG

任何帮助将不胜感激。

我将按 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'