I have a Pandas Series containing Several strings Patterns as below:
JavaScript
x
17
17
1
stringsToSplit = ['6 Wrap',
2
'1 Salad , 2 Pepsi , 2 Chicken Wrap',
3
'1 Kebab Plate [1 Bread ]',
4
'1 Beyti Kebab , 1 Chicken Plate [1 Bread ], 1 Kebab Plate [1 White Rice ], 1 Tikka Plate [1 Bread ]',
5
'1 Kebab Plate [1 Bread , 1 Rocca Leaves ], 1 Mountain Dew '
6
]
7
8
s = pd.Series(stringsToSplit)
9
s
10
11
0 6 Wrap
12
1 1 Salad , 2 Pepsi , 2 Chicken Wrap
13
2 1 Kebab Plate [1 Bread ]
14
3 1 Beyti Kebab , 1 Chicken Plate [1 Bread ],
15
4 1 Kebab Plate [1 Bread , 1 Rocca Leaves ], 1...
16
dtype: object
17
I would like to split and explode it such that the result would be as follows:
JavaScript
1
12
12
1
0 6 Wrap
2
1 1 Salad
3
1 2 Pepsi
4
1 2 Chicken Wrap
5
2 1 Kebab Plate [1 Bread ]
6
3 1 Beyti Keba
7
3 1 Chicken Plate [1 Bread ]
8
3 1 Kebab Plate [1 White Rice ]
9
3 1 Tikka Plate [1 Bread ]
10
4 1 Kebab Plate [1 Bread , 1 Rocca Leaves ]
11
4 1 Mountain Dew
12
In order to do the explode
I need to first split
. However, if I use split(',')
that also splits the items between []
which I do not want.
I have tried using split using regex but was not able to find the correct pattern.
I would appreciate the support.
Advertisement
Answer
You can use a regex with a negative lookahead:
JavaScript
1
2
1
s.str.split(r's*,(?![^[]]*])').explode()
2
output:
JavaScript
1
13
13
1
0 6 Wrap
2
1 1 Salad
3
1 2 Pepsi
4
1 2 Chicken Wrap
5
2 1 Kebab Plate [1 Bread ]
6
3 1 Beyti Kebab
7
3 1 Chicken Plate [1 Bread ]
8
3 1 Kebab Plate [1 White Rice ]
9
3 1 Tikka Plate [1 Bread ]
10
4 1 Kebab Plate [1 Bread , 1 Rocca Leaves ]
11
4 1 Mountain Dew
12
dtype: object
13