jeudi 18 juin 2020

Pandas if-statement multiple conditions [duplicate]

df = pd.DataFrame(data={'Countries':["US","GB,"FR"], 'Frequency':[20,30,34]})

I need to create another column 'Group' where:

if Country == "US" or "GB", then 'Group 1' if Country == "FR", then 'Group 2' else 'Group 3'

Output

df = pd.DataFrame(data={'Countries':["US","GB,"FR"], 'Frequency':[20,30,34], 'Group':['Group 1', 'Group 1', 'Group 2']})

I can't seem to find a way to do this, and I have a list of around 100 countries to do, but I already have them on a python list, so just need to find the syntax.

Thanks!

Aucun commentaire:

Enregistrer un commentaire