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.