Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply a split and take second element of result in Pandas column that sometimes contains None and sometimes does not split into more than 1 component

Tags:

python

pandas

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!

like image 742
helloB Avatar asked Mar 11 '23 07:03

helloB


1 Answers

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
like image 191
shivsn Avatar answered May 01 '23 03:05

shivsn