I have the following DataFrame in pandas:
| code | town | village | city |
|---|---|---|---|
| 01 | Brunete | NaN | NaN |
| 02 | NaN | Cabrera d'Anoia | NaN |
| 03 | NaN | NaN | Barcelona |
| 04 | Zarzalejo | NaN | Madrid |
| 07 | Melilla | NaN | City of Melilla |
| 08 | Cartagena | Galifa | Region of Murcia |
I want to merge the columns town, city and village into one. In case more than 1 column contains a value other than NaN, the resulting column will be assigned the value in this order of preference: village > town > city. Example:
| code | merged_column |
|---|---|
| 01 | Brunete |
| 02 | Cabrera d'Anoia |
| 03 | Barcelona |
| 04 | Zarzalejo |
| 07 | Melilla |
| 08 | Galifa |