Using like statement in pandas df - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Using like statement in pandas df (/thread-36246.html) |
Using like statement in pandas df - bnadir55 - Feb-03-2022 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 RE: Using like statement in pandas df - jefsummers - Feb-03-2022 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
|