Python Forum

Full Version: Using like statement in pandas df
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,
I have a function in which I want to return in a column all the values that like ABC and return it in a new column as ABC , but I get an error, please advise, Thx!

def Dest_new (row):
if row(df_3.destination_location.str.startswith('ABC')) == True:
return 'ABC'
return 'Other'

df_3['Dest_New'] = df_3.apply(Dest_new, axis=1)

error :
TypeError: 'Series' object is not callable
You made major changes in your post from original - better to post a reply to your own - you never know if someone is working on it.
Here, I believe this works for you.
import pandas as pd

df = pd.DataFrame(['abcdefg','bcde','abcfoo','defgh'], columns=['A'])

def fun(the_col):
    if 'abc' in the_col.A:
        return 'abc'
    elif 'de' in the_col.A:
        return 'de'
    else:
        return 'foo'
    return

dfA = df.apply(fun, axis = 1)
dfA
Output:
0 abc 1 de 2 abc 3 de