python - 在多个条件下为一列中的每一行提取第一个单词
问题描述
我有一个数据集包含一列字符串。看起来像
df.a=[['samsung/windows','mobile unknown','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])
推荐阅读
- android - build.gradle中testImplementation和androidTestImplementation之间的android区别
- c++ - 使用 task_group 的英特尔线程构建模块性能不佳(新用户)
- android - Kotlin - Android 中的自定义对话框
- php - 将 mySQL 查询转换为 QueryBuilder
- optaplanner - 使用 OptaPlanner 的复合任务
- javascript - Electron 在更新组件样式时意外工作
- asp.net-core - ASP.NET Core:未找到 global.json 版本 2.1.400 的兼容 SDK 版本
- java - 从 Java Async 调用 Kotlin 代码
- php - ffmpeg 在终端中工作,但不在 php 中
- typescript - Typescript 对象属性类型