简体   繁体   English

使用 pandas 删除句子后面/里面的所有内容

[英]using pandas to remove everything that comes after / inside sentences

I have a csv file containing sentences that some has / in like shown in the picture我有一个 csv 文件,其中包含一些有 / 的句子,如图所示

在此处输入图像描述

I'm trying to go though all sentences and whenever a / comes just remove what every comes after it ny expected output is something like that我正在尝试 go 尽管所有句子和每当 / 出现时只需删除它后面的所有内容

在此处输入图像描述

what I tried until now is finding the slashes but how to remove what comes after them.到目前为止,我尝试的是找到斜线,但如何删除它们之后的内容。

tdata = pd.read_csv(fileinput)

pat = '[' + re.escape("/") + ']'
df=tdata[tdata['sentences'].str.contains(pat, regex=True, na=False)]

Use Series.str.split with one or no spaces \s* and / and then select first lists by str[0] :使用Series.str.split有一个或没有空格\s*/然后 select 首先列出str[0]

tdata['English'] = tdata['English'].str.split('\s*/').str[0]

Or you can remove possible whitepaces after by Series.str.strip :或者您可以在Series.str.strip之后删除可能的空白:

tdata['English'] = tdata['English'].str.split('/').str[0].str.strip()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM