samedi 23 octobre 2021

Key and Attribute Error with Pandas apply on a condition

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.

df2["Deversement Service"] = df2.swifter.apply(conditions, axis=1)

Conditions are like this :

elif df2[df2["Symptom"].str.contains("passw|account").any()] : return accounts(df2)

Where account is full of conditions like this :

    elif        df2[df2["Short description"] .str.contains("passe").any() & df2["Trigram MyPortal"].str.contains("FR-B2B/COF/HQ").any()]  :
     if     df2[df2["Symptom"]           .str.contains("Onedrive").any()]                                                             : return "* Onedrive B2C"
     elif   df2[df2["Short description"] .str.contains("Onedrive").any()]                                                             : return "* Onedrive B2C"
     elif   df2[df2["Impacted Service/Business Service (a partir de 6/2019)"].str.contains("PDT101").any()]                           : return "PDT101 - WORKSTATION"
     else:                                                                                                                            return "* AD D70 - LOCAL IT COFELY"

I can't use np.select or np.where. I got two errors :

KeyError: True

But also :

AttributeError: 'str' object has no attribute 'str'

Do you have any idea to solve this ?

Aucun commentaire:

Enregistrer un commentaire