location = ['Seoul', 'Busan', 'Jeju']
df = df.withColumn('new_key_location',
F.when(F.col('key_location').isin(location),
F.col('key_location')).otherwise(F.lit('Others')))
I created a new column called 'new_key_location', and except for locations Seoul, Busan, and Jeju, I named other locations as others.
However, I have a different column called 'location name'. There are also Seoul, Busan, and Jeju, but they were named as others in the new_key_location column.
How can I change 'others' in the new_key_location(newly created column) based on Seoul, Busan, and Jeju in the column 'location name.'
| key Location | location name | new_key_location |
|---|---|---|
| Others | Seoul | Others → Seoul |
| Others | Others | Others |
| Seoul | Seoul | Seoul |
| Others | Busan | Others → Busan |