gpt4 book ai didi

python - 在 Pandas 列中应用拆分并获取结果的第二个元素,该列有时包含 None 并且有时不会拆分为超过 1 个组件

转载 作者:太空狗 更新时间:2023-10-30 02:17:46 26 4
gpt4 key购买 nike

我正在查看如下所示的电子邮件发件人数据列:

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 吗?我怎样才能实现我想要的并解决上述问题?

谢谢!

最佳答案

你可以做到 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

关于python - 在 Pandas 列中应用拆分并获取结果的第二个元素,该列有时包含 None 并且有时不会拆分为超过 1 个组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39171399/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com