Search code examples
pythonpandaspivottabular

Create new columns in a dataframe with combination of three columns in python


I have something like this:

       Date  Id  Product  Sales
0  1/1/2001   1       21      1200
1  1/1/2001   1       22      1000
2  1/1/2001   1       23      1500
3  2/1/2001   1       21      300
4  2/1/2001   2       22      200
5  3/1/2001   3       21      400
6  4/1/2001   3       22      500

I want to create something like this with same table:

enter image description here


Solution

  • You would concatenate ID and Product, and then pivot the result.

    import pandas as pd
    
    df=pd.DataFrame([['1/1/2001',1,21,1200],['1/1/2001',1,22,1000],['1/1/2001',1,23,1500],['2/1/2001',1,21,300],['2/1/2001',2,22,200],['3/1/2001',3,21,400],['4/1/2001',3,22,500]],columns=('Date','Id','Product','Sales'))
    
    df['Id_Prod'] = df['Id'].astype(str).str.cat(df['Product'].astype(str), sep='_')
    
    df.pivot(index='Date',columns='Id_Prod',values='Sales')
    

    Result:

    enter image description here