
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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
- Related Articles
- Create a Pipeline and remove a column from DataFrame - Python Pandas
- Python - Remove duplicate values from a Pandas DataFrame
- Python - How to select a column from a Pandas DataFrame
- Python – Create a new column in a Pandas dataframe
- Python – Drop a level from a multi-level column index in Pandas dataframe
- How to delete a column from Pandas DataFrame
- Python – Drop multiple levels from a multi-level column index in Pandas dataframe
- Python - Add a zero column to Pandas DataFrame
- Python - Stacking a multi-level column in a Pandas DataFrame
- How to delete a column from a Pandas DataFrame?
- Apply uppercase to a column in Pandas dataframe in Python
- Create a Pipeline and remove a row from an already created DataFrame - Python Pandas
- Python Pandas - Subset DataFrame by Column Name
- Python Pandas – Display all the column names in a DataFrame
- Python – Center align column headers of a Pandas DataFrame

Advertisements