python - Pandas - 列中引号(“)符号后的单词可以大写吗?
问题描述
列中引号 (") 符号后的单词可以大写吗?
如下所示:
输入:
A | B
1 | Hi "john"
2 | My "mother" lives with my "father
输出:
A | B
1 | Hi "John"
2 | My "Mother" lives with my "Father
我正在尝试下一个语句但没有成功:
df['B'] = df['B'].apply('. '.join(map(lambda s: s.strip().title(), df['B'].astype(str).str.split('"')))
解决方案
这应该适用于多个单词:
df['B'] = df['B'].str.replace(r'"(.+)"', lambda m: m.group(0).title())
如果您需要更复杂的转换 - 只需定义一个自定义函数并将其插入lambda m
.
推荐阅读
- python - 计算 python 字典的特定键中的值的数量
- python - 将python嵌套的类似JSON的数据转换为数据框
- arrays - 如何在Ruby中考虑多个空格来拆分字符串
- r - R 中的随机森林错误:`[.data.frame`(data, , all.vars(Terms), drop = FALSE) 中的错误:选择了未定义的列
- python - Python 脚本到 c .obj 文件
- java - 在初始化 Winium Driver 时出现 Connection Refused Exception
- java - Gradle Fatjar 构建错误
- json - 运行集合时如何将 Postman 中的 API 请求响应保存到 JSON 文件
- arrays - 使用行(间接(字符串))不起作用
- c# - 连接到 SQL Server 数据库文件时遇到问题。我究竟做错了什么?