chasedcribbet
chasedcribbet

Reputation: 258

Pandas in df column extract string after colon if colon exits; if not, keep text

Looked for awhile on here, but couldn't find the answer.

df['Products'] = ['CC: buns', 'people', 'CC: help me'] 

Trying to get only text after colon or keep text if no colon is in the string.

Tried a lot of things, but this was my final attempt.

x['Product'] = x['Product'].apply(lambda i: i.extract(r'(?i):(.+)') if ':' in i else i)

I get this error:

Might take two steps, I assume.

I tried this:

x['Product'] = x['Product'].str.extract(r'(?i):(.+)')

Got me everything after the colon and a bunch of NaN, so my regex is working. I am assuming my lambda sucks.

Upvotes: 5

Views: 1231

Answers (2)

Andy L.
Andy L.

Reputation: 25269

Use str.split and get last item

df['Products'] =  df['Products'].str.split(': ').str[-1]

Out[9]:
    Products
0       buns
1     people
2    help me

Upvotes: 5

deadshot
deadshot

Reputation: 9071

Try this

df['Products'] = df.Products.apply(lambda x: x.split(': ')[-1] if ':' in x else x)
print(df)

Output:

  Products
0     buns
1   people
2  help me

Upvotes: 1

Related Questions