2

Good Day,

I have a Pandas DataFrame with a column 'Cityname' and another list of 'BigCities' For each row in the column 'Cityname' I would like to exchange the Cityname when the city is in the list of 'BigCities' to 1 or if not 0.

Can anyone help with that. I Have problems when it comes to two lists when comparing.

Thank you kindly.

Regards

Peter

Teymour
  • 1,832
  • 1
  • 13
  • 34

1 Answers1

0

You can check membership by list BigCities with Series.isin and then cast to integers:

df['Cityname'] = df['Cityname'].isin(BigCities).astype(int)

Or use numpy.where:

df['Cityname'] = np.where(df['Cityname'].isin(BigCities), 1, 0)
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252