首页 > 解决方案 > 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('"')))

标签: pythonpandas

解决方案


这应该适用于多个单词:

df['B'] = df['B'].str.replace(r'"(.+)"', lambda m: m.group(0).title())

如果您需要更复杂的转换 - 只需定义一个自定义函数并将其插入lambda m.


推荐阅读