0

I have a pandas dataframe having 3 columns in which 2 column contains text strings having comma-separated values. I want to split each CSV field of both the columns and create a new row per entry. For example, a should become b:

Input:
    var1   var2  var3 
0   a,b     1   12,13
1   c,d     2   15,16

Output:    
   var1  var2   var3
0    a     1     12 
1    b     1     12
2    a     1     13
3    b     1     13
4    c     2     15 
5    d     2     15
6    c     2     16
7    d     2     16

I have tried the below script but I am able to convert only column-1 CSV to rows along with column-2

pd.concat([pd.Series(row[1], row[0].split(',')) for _, row in df.iterrows()]).reset_index()

The output which I am getting is:

Output:    
   var1  var2   
0    a     1    
1    b     1        
2    c     2    
3    d     2    

Any help would be appreciated.

user2693313
  • 341
  • 3
  • 5
  • 13
  • 1
    Possible duplicate of [How to do 'lateral view explode()' in pandas](https://stackoverflow.com/questions/38428796/how-to-do-lateral-view-explode-in-pandas) – o-90 Aug 23 '17 at 17:36

1 Answers1

0

From the answer How to do lateral view explode() in pandas, adapted to your circumstances:

rows = []
for i, row in df.iterrows():
    for a in row.var1.split(','):
        for c in row.var3.split(','):
            rows.append([a, row.var2, c])

pd.DataFrame(rows, columns=df.columns)
jdg
  • 547
  • 6
  • 15