I imported a CSV file in Python. One of the fields is numeric with 11 digits. The first 2 digits all start with 27. How can i replace the 27 with a 0?
CodePudding user response:
Just try following code: df['column_name']=df['column_name'].replace([original_value], new_value) if you want to replace more than one values: df['column_name']=df['column_name'].replace([v1, v2, v3], [a, b, c])
CodePudding user response:
Let say your column looks like this
column_name
0 273525235
1 2724112414124
2 34214124124
astype('string')is converting that numeric column tostringfirstUsing
str.replace('27', '00')replace27with00and thenastype('int64')will convert that string column to numeric again.data['column_name'] = data['column_name'].astype('string').str.replace('27', '00').astype('int64')
Output
column_name
0 3525235
1 24112414124
2 34214124124
