Python Pandas – Remove numbers from string in a DataFrame column


To remove numbers from string, we can use replace() method and simply replace. Let us first import the require library −

import pandas as pd

Create DataFrame with student records. The Id column is having string with numbers −

dataFrame = pd.DataFrame(
   {
      "Id": ['S01','S02','S03','S04','S05','S06','S07'],"Name": ['Jack', 'Robin', 'Ted', 'Robin', 'Scarlett', 'Kat', 'Ted'],"Result": ['Pass', 'Fail', 'Pass', 'Fail', 'Pass', 'Pass', 'Pass']
   }
)

Remove number from strings of a specific column i.e. “Id” here −

dataFrame['Id'] = dataFrame['Id'].str.replace('\d+', '')

Example

Following is the code −

import pandas as pd

# Create DataFrame with student records
dataFrame = pd.DataFrame(
   {
      "Id": ['S01','S02','S03','S04','S05','S06','S07'],"Name": ['Jack', 'Robin', 'Ted', 'Robin', 'Scarlett', 'Kat', 'Ted'],"Result": ['Pass', 'Fail', 'Pass', 'Fail', 'Pass', 'Pass', 'Pass']
   }
)

print"DataFrame ...\n",dataFrame

# removing number from strings of a specific column
dataFrame['Id'] = dataFrame['Id'].str.replace('\d+', '')

print"\nUpdated DataFrame...\n", dataFrame

Output

This will produce the following output −

DataFrame ...
     Id      Name   Result
0   S01      Jack     Pass
1   S02     Robin     Fail
2   S03       Ted     Pass
3   S04     Robin     Fail
4   S05  Scarlett     Pass
5   S06       Kat     Pass
6   S07       Ted     Pass

Updated DataFrame...
   Id      Name   Result
0   S      Jack     Pass
1   S     Robin     Fail
2   S       Ted     Pass
3   S     Robin     Fail
4   S  Scarlett     Pass
5   S       Kat     Pass
6   S       Ted     Pass

Updated on: 20-Sep-2021

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements