pandassplitco

Splitting Column Headers and Duplicating Row Values in Pandas Dataframe


In the example df below, I'm trying to find a way to split the column headers ('1;2','4','5;6') based on the ';' that exists and duplicate the row values in these split columns. (My actual df comes from an imported csv file so generally I have around 50-80 column headers that need spliting)

Below is my code below with output

 import pandas as pd
 import numpy as np  
 #

 data = np.array([['Market','Product Code','1;2','4','5;6'],
            ['Total Customers',123,1,500,400],
            ['Total Customers',123,2,400,320],
            ['Major Customer 1',123,1,100,220],
            ['Major Customer 1',123,2,230,230],
            ['Major Customer 2',123,1,130,30],
            ['Major Customer 2',123,2,20,10],
            ['Total Customers',456,1,500,400],
            ['Total Customers',456,2,400,320],
            ['Major Customer 1',456,1,100,220],
            ['Major Customer 1',456,2,230,230],
            ['Major Customer 2',456,1,130,30],
            ['Major Customer 2',456,2,20,10]])

  df =pd.DataFrame(data)
  df.columns = df.iloc[0]
  df = df.reindex(df.index.drop(0))
  print (df)
0             Market Product Code 1;2    4  5;6
1    Total Customers          123   1  500  400
2    Total Customers          123   2  400  320
3   Major Customer 1          123   1  100  220
4   Major Customer 1          123   2  230  230
5   Major Customer 2          123   1  130   30
6   Major Customer 2          123   2   20   10
7    Total Customers          456   1  500  400
8    Total Customers          456   2  400  320
9   Major Customer 1          456   1  100  220
10  Major Customer 1          456   2  230  230
11  Major Customer 2          456   1  130   30
12  Major Customer 2          456   2   20   10

Below is my desired output

 0             Market Product Code   1   2      4      5    6
 1    Total Customers          123   1   1     500    400  400
 2    Total Customers          123   2   2     400    320  320
 3   Major Customer 1          123   1   1     100    220  220
 4   Major Customer 1          123   2   2     230    230  230
 5   Major Customer 2          123   1   1     130    30   30
 6   Major Customer 2          123   2   2     20     10   10
 7    Total Customers          456   1   1     500    400  400
 8    Total Customers          456   2   2     400    320  320
 9   Major Customer 1          456   1   1     100    220  220
10  Major Customer 1           456   2   2     230    230  230
11  Major Customer 2           456   1   1     130    30   30
12  Major Customer 2           456   2   2     20     10   10

Ideally I would like to perform such a task at the 'read_csv' level. Any thoughts?


Solution

  • Try reindex with repeat

    s=df.columns.str.split(';')
    df=df.reindex(columns=df.columns.repeat(s.str.len()))
    df.columns=sum(s.tolist(),[])
    df
    Out[247]: 
                  Market Product Code  1  2    4    5    6
    1    Total Customers          123  1  1  500  400  400
    2    Total Customers          123  2  2  400  320  320
    3   Major Customer 1          123  1  1  100  220  220
    4   Major Customer 1          123  2  2  230  230  230
    5   Major Customer 2          123  1  1  130   30   30
    6   Major Customer 2          123  2  2   20   10   10
    7    Total Customers          456  1  1  500  400  400
    8    Total Customers          456  2  2  400  320  320
    9   Major Customer 1          456  1  1  100  220  220
    10  Major Customer 1          456  2  2  230  230  230
    11  Major Customer 2          456  1  1  130   30   30
    12  Major Customer 2          456  2  2   20   10   10