In a pandas DataFrame I'm trying to relabel the two levels of a variable with one single name but leave the 'Nan' values in the variable untouched.
Below is a reproducible example using a modified version of the 'mtcars' dataset. Here I want to relabel the 'yes' and 'no' levels of the 'am' variable to 'new' for example.
mpg cyl disp hp drat wt qsec vs am
Mazda RX4 21.0 six 160.0 110 3.90 2.620 16.46 0 yes
Mazda RX4 Wag 21.0 two 160.0 110 3.90 2.875 17.02 0 NaN
Datsun 710 22.8 six 108.0 93 3.85 2.320 18.61 1 no
Hornet 4 Drive 21.4 two 258.0 110 3.08 3.215 19.44 1 NaN
Hornet Sportabout 18.7 six 360.0 175 3.15 3.440 17.02 0 yes
Valiant 18.1 two 225.0 105 2.76 3.460 20.22 1 NaN
Duster 360 14.3 two 360.0 245 3.21 3.570 15.84 0 no
Result would look like this:
mpg cyl disp hp drat wt qsec vs am
Mazda RX4 21.0 six 160.0 110 3.90 2.620 16.46 0 new
Mazda RX4 Wag 21.0 two 160.0 110 3.90 2.875 17.02 0 NaN
Datsun 710 22.8 six 108.0 93 3.85 2.320 18.61 1 new
Hornet 4 Drive 21.4 two 258.0 110 3.08 3.215 19.44 1 NaN
Hornet Sportabout 18.7 six 360.0 175 3.15 3.440 17.02 0 new
Valiant 18.1 two 225.0 105 2.76 3.460 20.22 1 NaN
Duster 360 14.3 two 360.0 245 3.21 3.570 15.84 0 new
Aucun commentaire:
Enregistrer un commentaire