vendredi 22 octobre 2021

How to create conditionnal columns in Pandas with any?

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.

Sample data :

df = pd.DataFrame([["ores"], ["ores + more texts"], ["anything else"]], columns=['Symptom'])
def conditions(df5):
    if ("ores") in df5["Symptom"]: return "Things"

df["new_column"] = df.swifter.apply(conditions, axis=1)

It's doesn't work because any("something") is always True

So i tried :

df['new_column'] = np.where(df2["Symptom"].str.contains('ores'), 'yes', 'no') : return "Things"

It doesn't work because it's inside a loop. I can't use np.select because it needed two separate lists and my code has to be easily editable (and it can't come from a dict). It also doesn't work with find_all. And also not with :

df["new_column"] == "ores" is True: return "things"

I don't really understand why nothing work and what i have to do ?

Aucun commentaire:

Enregistrer un commentaire