如何保存现有字符串的新列部分

问题描述 投票:1回答:1

在数据框中,我有一个包含电子邮件列表的列。我的经理要我在@之后和.之前保留这个名字到一个新专栏。我尝试了以下方法:

DF['newcolumn'] = DF['email'].split("@")[2].split(".")[0]

但它不起作用。有任何想法吗?

python text split
1个回答
1
投票

使用以下正则表达式作为分隔符:

df['email'].str.split('@|\.').str[-2]

MVCE:

df = pd.DataFrame({'email':['[email protected]',
                            '[email protected]',
                            '[email protected]']})

df['email'].str.split('@|\.').str[-2]

输出:

0         abc
1       candy
2    questinc
Name: email, dtype: object
© www.soinside.com 2019 - 2024. All rights reserved.