Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error while trying to replace multiple values of a pandas data-frame column based on matching condition

I have a column in a pandas data frame, among other columns, as such:

     Remarks
   Left_only
  Right_only
   Left_only
  Right_only

For this column, I want to replace all Left_only values to Yesterday And Right_only To Today

I use this code line:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

DF.loc[df[‘Remarks’] == ‘Left_only’, ‘Remarks’] = ‘Yesterday’

Similarly, for the other one. But I get this error:

Cannot setitem on a Categorical with a new category (Yesterday), set the categories first

What am I doing wrong ?

>Solution :

# create a dictionary to map the two values
d={'Left_only': 'Yesterday', 'Right_only':'Today'}
df['Remarks']=df['Remarks'].map(d)
df
0    Yesterday
1        Today
2    Yesterday
3        Today
Name: Remarks, dtype: object
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading