I am looking at an email sender data column that looks like this:
from_name
----------
Joe Smith, VP at Corp
Alice Brown
None
Helpdesk, McRay's Store
From this, I'd like to generate a column like this:
title_if_any
-------------
VP at Corp
None
None
McRay's Store
I've tried the following:
email_data['title_email_sender'] = email_data[email_data.from_name.isnull() == False][email_data.from_name.apply(lambda x: ',' in x)].from_name.apply(lambda x: x.split(',')[1])
But this generates the following error:
----> 2 email_data['title_email_sender'] = email_data[email_data.from_name.isnull() == False][email_data.from_name.apply(lambda x: ',' in x)].from_name.apply(lambda x: x.split(',')[1])
TypeError: argument of type 'NoneType' is not iterable
Shouldn't my first selection remove all NoneTypes? How can I achieve what I want and fix the above?
Thanks!
you can do str.split():
In[53]:df['title_if_any']=df.from_name.str.split(',',expand=True)[1]
In[54]:df
Out[54]:
0 VP at Corp
1 None
2 None
3 McRays Store
Name: 1, dtype: object
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With