我正在查看一封如下所示的电子邮件发件人数据列:
from_name
----------
Joe Smith, VP at Corp
Alice Brown
None
Helpdesk, McRay's Store
由此,我想生成一个这样的列:
title_if_any
-------------
VP at Corp
None
None
McRay's Store
我尝试了以下内容:
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])
但这会产生以下错误:
----> 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
我的第一个选择不应该删除所有NoneTypes吗?我怎样才能实现我想要的并解决上述问题?
谢谢!
答案 0 :(得分:7)
你可以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