Search code examples
pythonpandasorange

Converting pandas dataframe into basket type orange table


I have the following pandas dataframe and would like any pointers in converting this to an Orange table with a basket format:

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

I'll like to convert it to an orange table with Basket format similar to this:

Beef

Beef, Fish, Eggs

Beef, Oil, Salt, Pepper, MSG

Any help will be much appreciated.


Solution

  • I'd groupby the order_id and print the results:

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

    If you want the string:

    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'