Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

pandas

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.

like image 563
chasedcribbet Avatar asked Sep 11 '20 03:09

chasedcribbet


1 Answers

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
like image 142
Andy L. Avatar answered Sep 27 '22 15:09

Andy L.