python - 如何剪切特定单词后的字符串?

标签 python pandas

我有一列文本。如何剪切每个文本在术语show less之后的部分?

例如:

df = pd.DataFrame({'col':['hi there see less new way is comming','today is summer, see less , is a lovely day']})

看起来像:

col 
'hi there see less new way is comming'
'today is summer, see less , is a lovely day'

输出:

col 
'hi there'
'today is summer,'

最佳答案

您可以使用str.splitsee less上进行分割,然后使用str切片来获取第一部分。

df['col'] = df['col'].str.split('see less').str[0]

输出:

                 col
0          hi there 
1  today is summer, 

关于python - 如何剪切特定单词后的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70687083/

相关文章:

python - 如何计算 Pandas 滚动窗口的累积乘积?

Python 正则表达式子函数

python - 如何导入已从父脚本导入的库?

python - 通过删除 NaN 并向左移动值以减少列数来压缩 pandas DataFrame 中的数据

python - 将总列的百分比添加到 Pandas 数据透视表

r - 根据条件清理数据框

python - df.fillna 或 df.replace 没有消除我的 .csv 文件中的 NaN

python - 在运行时创建的方法中设置参数名称和默认值

python - 多次 fork 调用会导致 BlockingIOError

python - 键盘控制 Python