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

How do I rename items from a column according to criteria?

I have the following dataframe:

      import pandas as pd
      df = pd.DataFrame({'ID': [1,2,3,4,5],
                         'Name_columns': ['1', '2', '2022', '1', '1']})

      print(df)

      ID    Name_columns
       1       1
       2       2
       3       2022
       4       1
       5       1

I would like to replace the values in the "Name_columns" column. Where is 1 I would like to write "SENSOR" and where is 2 write "ACTUATOR". So I tried to do the following code using replace():

      df['Name_columns'] = df['Name_columns'].str.replace('1', 'SENSOR')
      df['Name_columns'] = df['Name_columns'].str.replace('2', 'ACTUADOR')

The resulting output obtained is:

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

      print(df)

      ID    Name_columns
      1      SENSOR
      2      ACTUADOR
      3      ACTUADOR0ACTUADORACTUADOR
      4      SENSOR
      5      SENSOR

I would like to ask how can I keep the implementation for values 1 and 2 and, if different from these values, how can I write "Invalid"?

>Solution :

Use a dictionary to map the values and fillna with a default string:

d = {'1': 'SENSOR', '2': 'ACTUADOR'}

df['Name_columns'] = df['Name_columns'].astype(str).map(d).fillna('Invalid')

NB. if the column is already a string, you an leave out the astype(str)
output:

   ID Name_columns
0   1       SENSOR
1   2     ACTUADOR
2   3      Invalid
3   4       SENSOR
4   5       SENSOR
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