Python pandas:删除字符串中分隔符后的所有内容 [英] Python pandas: remove everything after a delimiter in a string

查看:47
本文介绍了Python pandas:删除字符串中分隔符后的所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有数据框,其中包含例如:

供应商 a::ProductA"供应商 b::产品 A"供应商a::产品b"

我需要删除所有(包括)两个 :: 以便我最终得到:

供应商A"供应商b"供应商"

我尝试了 str.trim(似乎不存在)和 str.split 没有成功.实现这一目标的最简单方法是什么?

解决方案

您可以像平常使用 split 一样使用 pandas.Series.str.split.只需在字符串 '::' 上拆分,并索引从 split 方法创建的列表:

<预><代码>>>>df = pd.DataFrame({'text': ["vendor a::ProductA", "vendor b::ProductA", "vendor a::Productb"]})>>>df文本0 供应商 a::ProductA1 个供应商 b::ProductA2 供应商 a::Productb>>>df['text_new'] = df['text'].str.split('::').str[0]>>>df文本 text_new0 供应商 a::ProductA 供应商 a1 供应商 b::ProductA 供应商 b2 供应商 a::Productb 供应商 a

这是一个非熊猫解决方案:

<预><代码>>>>df['text_new1'] = [x.split('::')[0] for x in df['text']]>>>dftext text_new text_new10 供应商 a::ProductA 供应商 a 供应商 a1 供应商 b::ProductA 供应商 b 供应商 b2 供应商 a::Productb 供应商 a 供应商 a

以下是对上面 pandas 中发生的事情的分步说明:

#选择你想要的pandas.Series对象>>>df['文本']0 供应商 a::ProductA1 个供应商 b::ProductA2 供应商 a::Productb名称:文本,数据类型:对象# 使用 pandas.Series.str 允许我们实现普通"字符串方法#(如拆分)在一个系列上>>>df['文本'].str<pandas.core.strings.StringMethods 对象在 0x110af4e48># 现在我们可以使用 split 方法来拆分我们的 '::' 字符串.你会看到# 返回一系列列表(就像你在熊猫之外看到的一样)>>>df['text'].str.split('::')0 [供应商 A,产品 A]1 [供应商 b,产品 A]2 [供应商a,产品b]名称:文本,数据类型:对象# 再次使用pandas.Series.str 方法,我们将能够通过索引# 上一步返回的列表>>>df['text'].str.split('::').str<pandas.core.strings.StringMethods 对象在 0x110b254a8># 现在我们可以获取上面每个列表中的第一项作为我们想要的输出>>>df['text'].str.split('::').str[0]0 供应商1 供应商 b2 供应商名称:文本,数据类型:对象

我建议查看 pandas.Series.str docs,或者更好的是,在 Pandas 中处理文本数据.

I have data frames which contain e.g.:

"vendor a::ProductA"
"vendor b::ProductA"
"vendor a::Productb"

I need to remove everything (and including) the two :: so that I end up with:

"vendor a"
"vendor b"
"vendor a"

I tried str.trim (which seems to not exist) and str.split without success. what would be the easiest way to accomplish this?

解决方案

You can use pandas.Series.str.split just like you would use split normally. Just split on the string '::', and index the list that's created from the split method:

>>> df = pd.DataFrame({'text': ["vendor a::ProductA", "vendor b::ProductA", "vendor a::Productb"]})
>>> df
                 text
0  vendor a::ProductA
1  vendor b::ProductA
2  vendor a::Productb
>>> df['text_new'] = df['text'].str.split('::').str[0]
>>> df
                 text  text_new
0  vendor a::ProductA  vendor a
1  vendor b::ProductA  vendor b
2  vendor a::Productb  vendor a

Here's a non-pandas solution:

>>> df['text_new1'] = [x.split('::')[0] for x in df['text']]
>>> df
                 text  text_new text_new1
0  vendor a::ProductA  vendor a  vendor a
1  vendor b::ProductA  vendor b  vendor b
2  vendor a::Productb  vendor a  vendor a

Edit: Here's the step-by-step explanation of what's happening in pandas above:

# Select the pandas.Series object you want
>>> df['text']
0    vendor a::ProductA
1    vendor b::ProductA
2    vendor a::Productb
Name: text, dtype: object

# using pandas.Series.str allows us to implement "normal" string methods 
# (like split) on a Series
>>> df['text'].str
<pandas.core.strings.StringMethods object at 0x110af4e48>

# Now we can use the split method to split on our '::' string. You'll see that
# a Series of lists is returned (just like what you'd see outside of pandas)
>>> df['text'].str.split('::')
0    [vendor a, ProductA]
1    [vendor b, ProductA]
2    [vendor a, Productb]
Name: text, dtype: object

# using the pandas.Series.str method, again, we will be able to index through
# the lists returned in the previous step
>>> df['text'].str.split('::').str
<pandas.core.strings.StringMethods object at 0x110b254a8>

# now we can grab the first item in each list above for our desired output
>>> df['text'].str.split('::').str[0]
0    vendor a
1    vendor b
2    vendor a
Name: text, dtype: object

I would suggest checking out the pandas.Series.str docs, or, better yet, Working with Text Data in pandas.

这篇关于Python pandas:删除字符串中分隔符后的所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆