简体   繁体   中英

How to check string in string with not exact same values?

I'm working with Pandas. I need to create a new column in a dataframe according to conditions in other columns. I try to look for each value in a series if it contains a value (a condition to return text).This works when the values are exactly the same but not when the value is only a part of the value of the series

I also tried with str.contains but I never succeeded The code who works with exact values :

if ("something" in df2["Symptom"].values):
    print("yes")
else:
    print("no")

And i got a "no" when it's not the same values.

IIUC:

df2 = pd.DataFrame(data={'Symptom':["I am something", 'I am not', 'Something 2']})

if df2["Symptom"].str.contains('Something').any():
    print("yes")
else:
    print("no")

OUTPUT: YES

And If you do the following:

if df2["Symptom"].str.contains('Something').all():
    print("yes")
else:
    print("no")

OUTPUT: NO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM