Home > Enterprise >  Why i am getting error " 'str' object has no attribute 'str'"? I simpl
Why i am getting error " 'str' object has no attribute 'str'"? I simpl

Time:02-19

Why i am getting error " 'str' object has no attribute 'str'"? I simply wanted to check "confirmeddtc" in my column which contain a text

Below is my code:

def Check(df_merge):
   if (df_merge["status"].str.lower().str.contains("confirmeddtc")):
        return 1
   else:
        return 0

df_merge['Status_Result'] = df_merge.apply(Check, axis=1)
       

df_merge.to_csv('C:\\Users\\jawed.sheikh\\Desktop\\R\\Trial.csv', index = False)

CodePudding user response:

You should use in to check if a string contains something. This should work for you:

def Check(df_merge):
   if ("confirmeddtc" in df_merge["status"].lower()):
        return 1
   else:
        return 0

CodePudding user response:

I think it would be better to avoid .apply here:

df_merge['Status_Result'] = (
    df_merge['status'].str.lower().str.contains('confirmeddtc').astype(int)
)
  • Related