pandas DataFrame Manipulation
Learn how to update, add, delete, and change the index of a pandas DataFrame.
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.
# 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,
print(df.iloc[0, 0])
# Updating Apple to Kiwi
df.iloc[0, 0] = "Kiwi"
# Printing the DataFrame
print(df)Updating a column,
# 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,
# 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,
# 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.
# Looking at the shape of the DataFrame
print(df.shape)
print(df)# Adding a column
df["Sold"] = ["Yes", "No", "No"]
# Printing the DataFrame
print(df)# Using loc to create a new index label called 3
df.loc["3"] = ["Kiwi", "1kg", "300", "No"]
# Printing the DataFrame
print(df)# 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.
# 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)# 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.
# Printing the DataFrame
print(df)
# Setting one of the columns as an index
print(df.set_index("Fruit"))# Adding a new column
df["Weight"] = ["1kg", "3kg", "5kg"]
# Printing the DataFrame
print(df)# 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.
# 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)# Reflecting the change in the DataFrame
df.reset_index(inplace=True)
# Printing the DataFrame
print(df)