I have a dataframe with 5 rows and 5 columns that I would like to make a new dataframe with 1 row. I have tried working with pivots but I do not need the agg and I dont know what to try next.
I used Scott Boston's answer from this link to get this done. How to combine multiple rows into a single row with python pandas based on the values of multiple columns?
Starting table
month | ID | state | sales | customers |
---|---|---|---|---|
0 | 1234 | MN | 44 | 23 |
1 | 1234 | MN | 45 | 25 |
2 | 1234 | MN | 51 | 27 |
3 | 1234 | MN | 47 | 29 |
4 | 1234 | MN | 44 | 29 |
0 | 5555 | NY | 441 | 231 |
1 | 5555 | NY | 451 | 251 |
2 | 5555 | NY | 511 | 271 |
3 | 5555 | NY | 471 | 291 |
4 | 5555 | NY | 441 | 291 |
data = [['0', '1234','MN',44,23],['1', '1234','MN',45,25],['2', '1234','MN',51,27],['3', '1234','MN',47,29],['4', '1234','MN',44,29],['0', '5555','NY',441,231],['1', '5555','NY',451,251],['2', '5555','NY',511,27],['3', '5555','NY',471,291],['4', '5555','NY',441,291],]
t1 = pd.DataFrame(data, columns=['month', 'ID', 'state', 'sales', 'customers'])
Ideal Final table
ID | state | sales_0 | customer_0 | sales_1 | customer_1 | sales_2 | customer_2 | sales_3 | customer_3 | sales_4 | customer_4 |
---|---|---|---|---|---|---|---|---|---|---|---|
1234 | MN | 44 | 23 | 45 | 25 | 51 | 27 | 47 | 29 | 44 | 29 |
5555 | NY | 441 | 231 | 451 | 251 | 511 | 271 | 471 | 291 | 441 | 291 |
I would need this to work for millions of IDs and months up to 50