简体   繁体   English

str.contains pandas 返回 'str' 对象没有使用 pandas 的新属性 'contains'

[英]str.contains pandas returns 'str' object has no attribute 'contains' new to using pandas

I am new to Pandas and been looking at this for a few days now, at a point where I need help.我是 Pandas 的新手,并且在我需要帮助的时候已经研究了几天。 I have the following code and it is throwing the following error:我有以下代码,它抛出以下错误:

AttributeError: ("'str' object has no attribute 'contains'", 'occurred at index 239') AttributeError: ("'str' 对象没有属性 'contains'",'发生在索引 239')

To be honest, I'm not sure where to start debugging, aside from the stuff I've tried.老实说,除了我尝试过的东西之外,我不确定从哪里开始调试。 The culprit line is: if row.display_name.contains("(EU)", case=False, regex=False) but I'm not confident that line is the problem.罪魁祸首是: if row.display_name.contains("(EU)", case=False, regex=False)但我不确定那条线是问题所在。 I've tried changing between contains and find but same error.我试过在 contains 和 find 之间进行更改,但同样的错误。 Any help or pointers would be really helpful任何帮助或指示都会非常有帮助

  if not discard_known_id.empty:
            discard_known_id["partner"] = discard_known_id.apply(
                lambda row: "Non-inventory user" if (pd.isna(row.partner))
                else (
                    "Non-inventory user (site known)"
                    if row.display_name.contains("(EU)", case=False, regex=False)
                    else row.partner
                ),
                axis=1,
            )

Tried this as well, in addition to find instead of contains也试过这个,除了 find 而不是 contains

 if not discard_known_id.empty:
            discard_known_id["partner"] = discard_known_id.apply(
                lambda row: "Non-inventory user" if (pd.isna(row.partner))
                else (
                    "Non-inventory user (site known)"
                    if row.display_name.str.contains("(EU)", case=False, regex=False)
                    else row.partner
                ),
                axis=1,
            )

Try changing the line to:尝试将行更改为:

If "EU" in row.display_name.upper():

It looks like you are accessing a singular cell in which case I believe contains is only a pandas method and will not apply.看起来您正在访问一个单一的单元格,在这种情况下,我认为 contains 只是一种熊猫方法,不适用。

yes str object does not have contains method in core python.是的 str 对象在核心 python 中没有 contains 方法。

Pandas is a library which has very useful functionality called Series and DataFrame, Pandas是一个库,它具有非常有用的功能,称为 Series 和 DataFrame,

In Series class it has attribute ' str ', through which you can use contains method.在 Series 类中,它具有属性“ str ”,您可以通过该属性使用contains方法。

Example :示例

import pandas as pd 

# Creating the Series 
series1 = pd.Series(['New_Delhi', 'Pune', 'Mumbai', 'Agra', 'Lahore'])
sr[sr.str.contains('N')]

Output输出

0    New_Delhi
dtype: object

For more information about series.str contain method then visit official site of of pandas library: pandas.Series.str.contains有关 series.str 包含方法的更多信息,请访问 pandas 库的官方网站: pandas.Series.str.contains

There is not a contains function in Python that works the way you are trying to use it. Python 中没有一个 contains 函数可以按照您尝试使用它的方式工作。 There is a str.contains but that works on a series, not a string (hence your error).有一个str.contains但它适用于一个系列,而不是一个字符串(因此你的错误)。 Instead you can use in .相反,您可以in .

I'd also add that I think the structure of your code is making your life unnecessarily difficult.我还要补充一点,我认为您的代码结构使您的生活变得不必要地困难。 Split out your logic into a separate function so you can more easily see what is happening.将您的逻辑拆分为一个单独的函数,以便您可以更轻松地查看正在发生的事情。 Here is a proposed solution:这是一个建议的解决方案:

def update_name(row):
    if pd.isna(row.partner):
        return "Non-inventory user"
    elif "(EU)" in row['display_name'].upper():
        return "Non-inventory user (site known)"
    else:
        return row.partner


if not discard_known_id.empty:
    discard_known_id["partner"] = discard_known_id.apply(update_name, axis=1)

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

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