Home > Enterprise >  Replacing numeric value with a different numeric value
Replacing numeric value with a different numeric value

Time:01-21

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

  1. astype('string') is converting that numeric column to string first

  2. Using str.replace('27', '00') replace 27 with 00 and then

  3. astype('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
  •  Tags:  
  • Related