How to remove numbers from string terms in a pandas dataframe?

Given a pandas dataframe, we have to remove numbers from string terms. By Pranit Sharma Last updated : September 30, 2023

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

The string is a group of characters, these characters may consist of all the lower case, upper case, and special characters present on the keyboard of a computer system. A string is a data type and the number of characters in a string is known as the length of the string.

Problem statement

Suppose, we have a DataFrame with multiple columns among which we have a Name column which contains the name of the person with a specific code. All the columns of this DataFrame are of string type. We need to remove this specific code part from the Name column in each value.

Remove numbers from string terms in a pandas dataframe

For this purpose, we will first select the column (let it be df['Name']) on which we have to apply this operation by and then we will use the .str.replace() method by passing the RegEx query and space to be replaced. For selecting and remove the numbers use '\d+' as RegEx.

Let us understand with the help of an example,

Python program to remove numbers from string terms in a pandas dataframe

# Importing pandas package
import pandas as pd

# Creating dictionary
d = {

# Creating DataFrame
df = pd.DataFrame(d)

# Display Original DataFrames
print("Created DataFrame:\n",df,"\n")

# Removing numbers from Name
df['Name'] = df['Name'].str.replace('\d+', '')

# Display modified DataFrame
print("Modified DataFrame:\n",df)


The output of the above program is:

Example: Remove numbers from string terms

Python Pandas Programs »

Comments and Discussions!

Load comments ↻

Copyright © 2024 All rights reserved.