0

For e.g. to get a row out of dataframe if it contains certain text got result using following link

Select by partial string from a pandas DataFrame

df[df['A'].str.contains("hello")]

where hello is present in column A of dataframe or not

this command prints the output

but i want to apply an if else on result such that if (it contains): do this else: do that

if i directly apply if else i get an error

if(df[df['A'].str.contains("hello")]):
    print("present")   // or any action
else:
    print("Not present")

error:Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()

How to apply if else to result

G.ONE
  • 423
  • 1
  • 4
  • 13

1 Answers1

-1

Do this:

if any(df[df['A'].str.contains("hello")]):
    print("present")  # or any action
else:
    print("Not present")
Suhas Mucherla
  • 1,103
  • 1
  • 3
  • 17