Python 在“熊猫”列中应用“拆分并获取结果”的第二个元素,该元素有时不包含任何内容,有时不拆分为多个组件
我看到的电子邮件发件人数据列如下所示:Python 在“熊猫”列中应用“拆分并获取结果”的第二个元素,该元素有时不包含任何内容,有时不拆分为多个组件,python,pandas,Python,Pandas,我看到的电子邮件发件人数据列如下所示: 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 由此,我想生成如下列: from_name ---------- Joe Smith, VP at Corp Alice Brown None Helpdesk,
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
由此,我想生成如下列:
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
我的第一个选择不应该删除所有非类型吗?我如何实现我想要的并解决上述问题
谢谢 您可以执行以下操作:
expand=True很重要,因为如果不使用expand=True,则只会得到第二行。