madsthaks
madsthaks

Reputation: 2181

Having trouble splitting text due to extra commas

Here are a few examples:

Input

Col
"temp, temp2"
"name, inc., name2"

Output

Col_upd
["temp","temp2]
["name, inc.", "name2]

Right now, I'm using:

Col_upd.apply(lambda x: [i.lower().strip() for i in x.split(',')])

This fails in row 2 in the above example. I'm not sure what alternatives I have in this situation aside from your a dictionary.

Any suggestions would be really helpful.

Upvotes: 0

Views: 34

Answers (1)

Pinyi Wang
Pinyi Wang

Reputation: 862

If we can assume that there's no extra commas in the second part, you can try to use rsplit().

Col_upd.apply(lambda x: [i.lower().strip() for i in x.rsplit(',', 1)])

str.rsplit() lets you specify how many times to split.

Upvotes: 3

Related Questions