I would like to fill my "Verif" column with values depending on several conditions referring to several columns all in the same df. If "Reception" is empty and "Premier" is 'entrant radio' then "Verif" becomes 'utilisateur' ; If "Reception" is empty and "Premier" is different from 'entrant radio' then "Verif" becomes 'n° de renvoi' ; Else (=when "Reception" is not empty) write 'MEVO' in "Verif " However, the result in Verif is MEVO everywhere.
for a in ['Reception']:
for b in ['Premier']:
if a == "" and b == 'Entrant Radio':
df['Vérif'] = "utilisateur"
elif a == "" and b != 'Entrant renvoyé par le MSC':
df['Vérif'] = "n° de renvoi"
else:
df['Vérif'] = "MEVO"
Can anyone help please?
CodePudding user response:
I think you can use DataFrame.apply to assign value to 'Vérif'
column based on the result return by a lambda or function (get_verif
in below example). Inside the function, you can get the value of the row column using row['<column name>']
and return different values that satisfy specific conditions
import pandas as pd
# Sample data
df = pd.DataFrame({'Reception': ['not empty', '', '', ''], 'Premier': ['some string', 'Entrant Radio', 'Entrant renvoyé par le MSC', 'random string']})
def get_verif(row):
if row['Reception'] == "" and row['Premier'] == 'Entrant Radio':
return "utilisateur"
elif row['Reception'] == "" and row['Premier'] != 'Entrant renvoyé par le MSC':
return "n° de renvoi"
else:
return "MEVO"
df['Vérif'] = df.apply(get_verif, axis=1)
print(df)
CodePudding user response:
It may because of case sensitivity? "Entrant Radio" and "entrant radio" are not same values.
CodePudding user response:
Like this.
new_verif = []
for r, p in zip(df['Reception'], df['Premier']):
if r == "":
if p == 'entrant radio':
new_verif.append('utilisateur')
else:
new_verif.append('n° de renvoi')
else:
new_verif.append('MEVO')
df['Verif'] = new_verif
CodePudding user response:
It worked, thanks @tax evader. I used:
def get_verif(row):
if row['Reception'] == "" and row['Premier'] == 'Entrant Radio':
return "utilisateur"
elif row['Reception'] == "" and row['Premier'] != 'Entrant Radio':
return "n° de renvoi"
else:
return "MEVO"
Extraction_Entrant_Mobile['Vérif'] = Extraction_Entrant_Mobile.apply(get_verif, axis=1)
Extraction_Entrant_Mobile