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
#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


Messages In This Thread
Using like statement in pandas df - by bnadir55 - Feb-03-2022, 01:05 PM
RE: Using like statement in pandas df - by jefsummers - Feb-03-2022, 09:15 PM

Forum Jump:

User Panel Messages

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