I am trying to use Pandas pivot_table to move line items on my receipts as columns, leaving only one row per receipt. My main challenge is to name my new columns counting numbers as needed and fill with NaNs.
I am trying to make this:
customer_id receipt_id item_id
01 100 420
05 400 450
05 400 460
05 400 320
05 400 270
02 300 320
02 300 460
05 200 220
...look like this:
customer_id receipt_id 1 2 3 4 n..
01 100 420 NaN NaN NaN
05 400 450 460 320 270
02 300 320 460 NaN NaN
05 200 220 NaN NaN Nan
I have been trying to make it work, but I am not close.
pt = pd.pivot_table(df, values=["item_id"], index=["customer_id", "receipt_id"], columns=["item_id"], fill_value="NaN").reset_index()