在pandas中提取特定值后的文本

2024-04-28 09:10:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试提取包含文本数据的列中的值,如下所示:

create date:1953/01/01 | first author:REAGAN RL

如何从列中提取作者姓名并存储在新列中。 我尝试了以下方法:

df.str.extract("first author:(.*?)")

authorname=df['EntrezUID'].apply(lambda x:x.split("first author:"))。第二个成功了

如何使用正则表达式实现类似的功能


Tags: 数据方法文本dfdatecreateextractauthor
1条回答
网友
1楼 · 发布于 2024-04-28 09:10:36

你可以做:

## sample data
df = pd.DataFrame({'dd':['create date:1953/01/01 | first author:REAGAN RL','create date:1953/01/01 | first author:MEGAN RL']})

## output
df['names'] = df['dd'].str.extract(r'author\:(.*)')

print(df)
                                                dd      names
0  create date:1953/01/01 | first author:REAGAN RL  REAGAN RL
1   create date:1953/01/01 | first author:MEGAN RL   MEGAN RL

相关问题 更多 >