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:
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"?