Python - Replace string/value in entire dataframe

Given a pandas dataframe, we have to perform Logical operation on two columns of the dataframe. By Pranit Sharma Last updated : September 29, 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.

A 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 of the student name and their subjects and we want to change the entire DataFrame with details of the product and their sales.

Replacing string/value in entire dataframe

For this purpose, we will use pandas.DataFrame.replace() method inside which we will pass the entire list of elements with which we want to replace the values of the original DataFrame.

Let us understand with the help of an example,

Python program to replace string/value in entire dataframe

# Importing pandas package
import pandas as pd

# Creating two dictionaries
d1 = {

# Creating DataFrame
df = pd.DataFrame(d1)

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

# Replacing DataFrame
result = df.replace(
    ['Rohit', 'Mohit', 'Shobhit','Raunak'],
    ['Hyundai', 'Toyota', 'Mahindra','Suzuki']

result = df.replace(

# Renaming the columns
result.columns = ['Brand','Sales']

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


The output of the above program is:

Example: Replace string/value in entire dataframe

Python Pandas Programs »

Comments and Discussions!

Load comments ↻

Copyright © 2024 All rights reserved.