【问题标题】:Python pandas: remove everything after a delimiter in a stringPython pandas:删除字符串中分隔符后的所有内容
【发布时间】:2017-04-03 23:32:36
【问题描述】:

我的数据框包含例如:

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

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

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

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

【问题讨论】:

  • str.split 可以做到——你是怎么尝试的?

标签: python python-3.x pandas


【解决方案1】:

如果它在数据框(名称:数据框)的特定列(名称:列)中,也可以使用

dataframe.column.str.replace("(::).*","")

它给你下面的结果

         column        new_column       
0  vendor a::ProductA  vendor a
1  vendor b::ProductA  vendor b
2  vendor a::Productb  vendor a

通过使用它,您无需指定任何位置,因为它消除了 '::' 之后的任何内容

我想这可能会帮到你,祝你好运!

【讨论】:

    【解决方案2】:

    您可以使用str.replace(":", " ") 删除"::"。 要拆分,需要指定要拆分成的字符:str.split(" ")

    trim函数在python中称为strip:str.strip()

    此外,您可以通过 str[:7] 在字符串中获取 "vendor x"

    祝你好运

    【讨论】:

    • 不能做 str[:7] 因为供应商名称的长度不同
    【解决方案3】:

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

    >>> 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
    

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

    >>> 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
    

    编辑:这是上面pandas中发生的事情的分步说明:

    # 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
    

    我建议查看pandas.Series.str docs,或者更好的是Working with Text Data in pandas

    【讨论】:

    • 我之前试过 str.split('::') 没有成功。 .str[0] 最后做了什么?
    • 它接受每个列表的第一个参数并返回它(str[0] = "vendor a", "vendor b", "vendor c", str[1] 将是 "ProductA", “产品B”,“产品C”)
    • 如何将它应用于多个列?
    猜你喜欢
    • 2012-10-19
    • 2017-06-03
    • 1970-01-01
    • 2019-10-11
    • 1970-01-01
    • 1970-01-01
    • 2012-08-29
    • 2012-03-08
    相关资源
    最近更新 更多