Python Forum
Using like statement in pandas df
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using like statement in pandas df
#1
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
Reply
#2
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
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020