Search code examples
pythonpandasdataframetranspose

how to convert pandas data frame rows into columns


I have the data frame like

df.to_dict('list')

output:

{'ChannelPartnerID': [10000,
  10000,
  10000,
  10000,
  10000,
  10001,
  10001,
  10001,
  10002,
  10002],
 'Brand': ['B1', 'B2', 'B3', 'B4', 'B5', 'B1', 'B2', 'B5', 'B1', 'B4'],
 'Sales': [29630, 38573, 1530, 21793, 7155, 26477, 42158, 14612, 6649, 6468]}

.

df

output:

ChannelPartnerID    Brand   Sales
0   10000           B1      29630
1   10000           B2      38573
2   10000           B3      1530
3   10000           B4      21793
4   10000           B5      7155
5   10001           B1      26477
6   10001           B2      42158
7   10001           B5      14612
8   10002           B1      6649
9   10002           B4      6468

I want to group the data by 'ChannelPartnerID' because I want to merge the data by unique values in unique value in 'ChannelPartnerID' and convert 'Brand' column values into columns containing Sales price for each column

I want it to be like this output:

ChannelPartnerID B1_Sales B2_Sales B3_Sales B4_Sales B5_Sales B6_Sales B7_Sales
0   10000        29630    38573    1530     21793    7155     0         0 
1   10001        26477    42158    0        0        14612    0         0 
2   10002        6649     0        0        6468     0        0         0 


Solution

  • What you want is a called a pivot:

    df.pivot(*df).fillna(0).add_suffix('_Sales')
    

    output:

    Brand             B1_Sales  B2_Sales  B3_Sales  B4_Sales  B5_Sales
    ChannelPartnerID                                                  
    10000                29630     38573      1530     21793      7155
    10001                26477     42158         0         0     14612
    10002                 6649         0         0      6468         0
    

    NB. df.pivot(*df) is a shortcut for df.pivot(index='ChannelPartnerID', columns='Brand', values='Sales')