1

i am trying to apply the custome function on pandas dataframe and its not working.

it giving me an error

AttributeError: 'str' object has no attribute 'str'

def f_MI(DF):
    if DF['AccNo'] == DF['Map_AccNo']:
        return -1
    else:
        if DF['AccNo'].str.lower().str[:3] == DF['Map_MainDesc'].str.lower().str[:3]:
            return 1
        else:
            return 0
    
z2['Inter_Type'] = z2.apply(f_MI,axis=1)
1
  • Try using __str__() instead of str before the lower() Commented Jun 15, 2022 at 11:24

1 Answer 1

0

Try to remove .str:

def f_MI(row):
    if row["AccNo"] == row["Map_AccNo"]:
        return -1
    else:
        if row["AccNo"].lower()[:3] == row["Map_MainDesc"].lower()[:3]:
            return 1
        else:
            return 0


z2["Inter_Type"] = z2.apply(f_MI, axis=1)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.