lundi 5 juillet 2021

How to apply multiple condition on rows without changing the result from the previous condition?

I have pandas dataframe where i need to apply multiple conditions. Here is the sample of my df

Cond           Samp        
A_B_C          Org
A_B_C          Org
A_B_C          Sea
A_B_C          Paid

I need a new column based on this condition

df['New'] = df[df['Samp'] == 'Org']['Cond'].str.rsplit("_", expand=True)[0]
df['New'] = df[df['Samp'] == 'Sea']['Cond'].str.rsplit("_", expand=True)[0]
df['New'] = df[df['Samp'] == 'Paid']['Cond'].str.rsplit("_", expand=True)[2]

desired output:

Cond           Samp        New
A_B_C          Org         A
A_B_C          Org         A
A_B_C          Sea         A
A_B_C          Paid        C

Aucun commentaire:

Enregistrer un commentaire