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"?
CodePudding user response:
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
CodePudding user response:
A pandas.Series
has a map
method that accepts a dictionary as an argument, and leaves the keys not on the dictionary as nan
df['Name_columns'].map({'1': 'SENSOR', '2': 'ACTUATOR'}).fillna('INVALID')
# 0 SENSOR
# 1 ACTUATOR
# 2 INVALID
# 3 SENSOR
# 4 SENSOR