gpt4 book ai didi

python - 在多种条件下提取列中每行的第一个单词

转载 作者:太空宇宙 更新时间:2023-11-03 20:34:30 25 4
gpt4 key购买 nike

我有一个数据集,其中包含一列字符串。看起来像

df.a=[['三星/Windows','移动未知','chrome/android']]。我试图获取每行的第一个单词来替换当前字符串,例如[['samsung','mobile','chrome']]

我申请了:

df.a=df.a.str.split().str.get(0)

这给了我第一个单词,但带有“/”

df.a=[words.split("/")[0] for words in df.a]

这只分割包含“/”的字符串

我可以用一行得到预期的结果吗?

最佳答案

使用re.findall()并仅获取字母数字

import re
df['a'] = df['a'].apply(lambda x : re.findall(r"[\w']+",x)[0])

关于python - 在多种条件下提取列中每行的第一个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57256760/

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