pandas DataFrame Manipulation

Learn how to update, add, delete, and change the index of a pandas DataFrame.

15 min read
Beginner

pandas DataFrame Manipulation

Learn to manipulate a pandas DataFrame including how to update, add and delete data from a pandas DataFrame as well as how to change the index of a pandas DataFrame.


How to update the data in a pandas DataFrame?

The values in a pandas DataFrame can be changed once created by selecting the rows or columns or both and assigning a new value.

python
# Importing the pandas library as pd
import pandas as pd

# Creating a Python Dictionary
dictionary = {
    "Fruit": ["Apple", "Orange", "Mango"],
    "Weight": ["1kg", "2kg", "5kg"],
    "Price": [300, 150, 800],
}

# Creating a Pandas DataFrame
df = pd.DataFrame(dictionary)

# Printing the DataFrame
print(df)

Updating a single value,

python
print(df.iloc[0, 0])

# Updating Apple to Kiwi
df.iloc[0, 0] = "Kiwi"

# Printing the DataFrame
print(df)

Updating a column,

python
# Selecting the first column
print(df.iloc[:, 0])

# Updating the first column
df.iloc[:, 0] = ["Banana", "Watermelon", "Peach"]

# Printing the DataFrame
print(df)

Updating a row,

python
# Selecting the first row
print(df.iloc[0])

# Updating the first row
df.iloc[0] = ["Apple", "1kg", "600"]

# Printing the DataFrame
print(df)

Updating multiple rows and columns,

python
# Selecting the first two rows and columns
print(df.iloc[:2, :2])

# Updating the first two rows and columns
df.iloc[:2, :2] = [["Banana", "3kg"], ["Avocado", "5kg"]]

# Printing the DataFrame
print(df)

How to add or delete data from a pandas DataFrame?

You can add or delete data from a pandas DataFrame by adding/removing a row or a column.

When adding a new row or column in a pandas DataFrame, the length of the insert should match the DataFrame's length on the row and column axis respectively.

python
# Looking at the shape of the DataFrame
print(df.shape)
print(df)
python
# Adding a column
df["Sold"] = ["Yes", "No", "No"]

# Printing the DataFrame
print(df)
python
# Using loc to create a new index label called 3
df.loc["3"] = ["Kiwi", "1kg", "300", "No"]

# Printing the DataFrame
print(df)
python
# Using loc to create a new index label called 3
df.loc["Random"] = ["Orange", "2kg", "400", "Yes"]

# Printing the DataFrame
print(df)

To delete rows or columns from a pandas DataFrame, you can use the drop() method from a pandas DataFrame.

python
# Dropping a column
print(df.drop(["Sold"], axis=1))

# Dropping multiple columns
print(df.drop(["Sold", "Weight"], axis=1))

# Dropping multiple columns inplace
df.drop(["Sold", "Weight"], axis=1, inplace=True)

# Printing the DataFrame
print(df)
python
# Dropping a row
print(df.drop(["Random"], axis=0))

# Dropping multiple rows
print(df.drop(["Random", "3"], axis=0))

# Dropping multiple rows inplace
df.drop(["Random", "3"], axis=0, inplace=True)

# Printing the DataFrame
print(df)

How to set and remove an index in a pandas DataFrame

You can set the index of a pandas DataFrame by using the set_index() method off of a pandas DataFrame.

python
# Printing the DataFrame
print(df)

# Setting one of the columns as an index
print(df.set_index("Fruit"))
python
# Adding a new column
df["Weight"] = ["1kg", "3kg", "5kg"]

# Printing the DataFrame
print(df)
python
# Setting multiple columns as an index
print(df.set_index(["Fruit", "Price"]))

# Reflecting the change in the DataFrame
df.set_index(["Fruit", "Price"], inplace=True)

# Printing the DataFrame
print(df)

To remove the current index and reset it with a new one, you can use the reset_index() method off of a pandas DataFrame.

python
# Resetting the index at level 0
print(df.reset_index(level=0))

# Resetting the index at level 1
print(df.reset_index(level=1))

# Resetting the index
print(df.reset_index())

# Printing the DataFrame
print(df)
python
# Reflecting the change in the DataFrame
df.reset_index(inplace=True)

# Printing the DataFrame
print(df)