0

This is my first post so if it's not detailed enough, please let me know! (sorry for my english)

I've been looking for 2 days now and I don't see how to do it...

As I wrote in the title, I have a dateframe with multiple rows that have the same code but with other values in the other columns. I want to aggregate all the rows which have the same code and put the different columns in the aggregated row.

I have this dataframe for exemple :

df = pd.DataFrame({
    'code': [1, 1, 1, 2, 2, 3, 4, 4], 
    'amount': [100, 0.20, 120, 50, 31, 68, 687, 54],
    'item': ['ITEM0001', 'ITEM0002', 'ITEM0003', 'ITEM0004', 'ITEM0005', 'ITEM0006', 'ITEM0007', 'ITEM0008']
})

I have to group by code, so i did this :

df2 = df.groupby('code', as_index = False).agg({"amount" : list, "item" : list})
     code        amount                   item
0     1  [100.0, 0.2, 120.0]  [ITEM0001, ITEM0002, ITEM0003]
1     2         [50.0, 31.0]            [ITEM0004, ITEM0005]
2     3               [68.0]                      [ITEM0006]
3     4        [687.0, 54.0]            [ITEM0007, ITEM0008]

I hope to convert dataframe to the following format:

     code         item1        amount1        item2        amount2        item3         amount3
0     1          ITEM0001       100.0        ITEM0002        0.2         ITEM0003        120.0
1     2          ITEM0004        50.0        ITEM0005       31.0             NONE         NONE
2     3          ITEM0006        68.0            NONE       NONE             NONE         NONE
3     4          ITEM0007       687.0        ITEM0008       54.0             NONE         NONE

For the example, i have just 3 items max (for the code 1 here) but, i can have 50 or more differents items.

I tried to use explode() but it's not that i want..

Thanks in advance, have a good day!

Riheii
  • 1

0 Answers0