I have a dataframe
where there is a named column Valoración
that contains three types of variables, bien
, mal
or regular
. I would like to convert the and values to mal
and regular
so no_bien
this way it treats mal
and regular
as a single value.
I do the following but it doesn't work,
new_df.at['Valoracion']['mal'] = 'no_bien'
new_df.at['Valoracion']['regular'] = 'no_bien'
new_df = new_df.dropna()
new_df['Valoracion'].value_counts().plot(kind = 'bar')
How can I do it?
Thank you
A humble solution could be using lambda . Such that it returns
bien
yes the Rating isbien
, andno_bien
along the wayfalso
.We can create a new column just to check that the results are correct.
and the output would be like this:
ps: This solution will work for you as long as you have 3 different values as in your case good, bad, regular
Edit: You can replace the values by mapping or making a correspondence between the value of the column and the value that should return associated with it. . In your case we do:
'good' corresponds to 'good'
'bad' corresponds to 'not-good'
'regular' corresponds to 'not-good'
And we run the mapping for that column. Since we want to modify the column we make the result the same column.
I think the easiest way that occurred to me is with ".loc", leaving this simple line:
To everything in the 'Rating' column that is other than good, we assign 'not_good'
Arm the df as Lucas in his answer
resulting in the following:
For this you could use pandas.DataFrame.loc , identifying the element and then replacing it with the value you need in this case no_okay and finally saving the file. In the following way:
I hope it helps you. Cheers ;)