Modifying Data in Pandas
Last Updated: 29th August 2025
What you'll learn
- How to change values in a DataFrame or Series
- How to replace values in a DataFrame or Series
replace(): Replace specific values with another valuewhere(): Replace values based on a condition.mask(): Replace values based on a boolean mask.- How to add or remove rows or columns
- Rename columns,index etc
Let Data is like this
import pandas as pd
df = pd.DataFrame({
"Name": ["Amit", "Ravi", "Neha"],
"Age": [25, 30, 22],
"Marks": [85, 90, 95]
})
Modifying DataFrame
Change a Single Value
# Change one value using loc
df.loc[0, "Age"] = 26 # Amit's Age becomes 26
# Change one value using iloc
df.iloc[1, 2] = 92 # Ravi's Marks become 92
print(df)
Update Entire Column
# Increase all ages by 1
df["Age"] = df["Age"] + 1
print(df)
Change Multiple Values
# Change multiple rows/columns
df.loc[0:1, "Marks"] = [88, 93] # Amit=88, Ravi=93
print(df)
df.loc[0:2,["Marks","Age"]]=[[97,21],[85,12]]
print(df)
Replace values
# Old Method
df["Age"].replace({25: 26, 30: 31}, inplace=True) # Replace 25 with 26 and 30 with 31
print(df)
# New Method
df.replace({"Age": {25: 26, 30: 31}}, inplace=True) # Replace 25 with 26 and 30 with 31
print(df)
# Change in whole Data set
df.replace("Amit","amit",inplace=True)
print(df)
df.replace({"Name":"amit","Age":25},None,inplace=True)
print(df)
df.replace({None:"Unknown"})
print(df)
Update Column with condition
# Increase marks by 5 where Age > 25
df.loc[df["Age"] > 25, "Marks"] += 5
print(df)
# Replace values based on a condition
print(df.where(df["Age"] > 25, "Unknown"))
# Replace values based on a boolean mask
print(df.mask(df["Age"] > 25, "Unknown"))
Adding / Removing columns & rows
Adding a new column
# Add new column "Address"
df["Address"] = ["Kashi", "Delhi", "Lucknow"]
print(df)
Add column with same value
df["Country"] = "India" # All rows get India
print(df)
Add column at specific position
# df.insert(index,newColName,Value)
df,insert(4,"ZipCode",[221010,213231,202020])
Adding Rows
# Add Single Row
new_row = {"Name": "Kiran", "Age": 28, "Marks": 88, "Country": "India","Address": "Noida"}
df = df._append(new_row, ignore_index=True) # _append in latest Pandas
print(df)
# Add Multiple Rows
new_rows = [
{"Name": "Sita", "Age": 24, "Marks": 91, "Country": "India","Address": "Delhi"},
{"Name": "Sadhu", "Age": 2, "Marks": 0, "Country": "India","Address": "Kashi"}
]
df = df._append(new_rows, ignore_index=True)
print(df)
Removing Columns
# Remove one column
df = df.drop("Country", axis=1)
# Remove multiple columns
df = df.drop(["Age", "Marks"], axis=1)
print(df)
Removing Rows
# Remove row by index (By default axis=0)
df = df.drop(1, axis=0) # removes 2nd row (Ravi)
# Remove multiple rows
df = df.drop([0, 2], axis=0)
print(df)
Rename and Reindex
1. Rename columns
# Rename single column
df = df.rename(columns={"Marks": "Score"})
# Rename multiple columns
df = df.rename(columns={"Age": "Years", "Name": "Student_Name"})
2. Rename index
# Rename index
df = df.rename(index={0: "A", 1: "B", 2: "C"})
3. Indexing
# Set index
df = df.set_index("Name")
print(df)
# Reset index
df = df.reset_index()
print(df)