
- Python Pandas - Home
- Python Pandas - Introduction
- Python Pandas - Environment Setup
- Python Pandas - Basics
- Python Pandas - Introduction to Data Structures
- Python Pandas - Index Objects
- Python Pandas - Panel
- Python Pandas - Basic Functionality
- Python Pandas - Indexing & Selecting Data
- Python Pandas - Series
- Python Pandas - Series
- Python Pandas - Slicing a Series Object
- Python Pandas - Attributes of a Series Object
- Python Pandas - Arithmetic Operations on Series Object
- Python Pandas - Converting Series to Other Objects
- Python Pandas - DataFrame
- Python Pandas - DataFrame
- Python Pandas - Accessing DataFrame
- Python Pandas - Slicing a DataFrame Object
- Python Pandas - Modifying DataFrame
- Python Pandas - Removing Rows from a DataFrame
- Python Pandas - Arithmetic Operations on DataFrame
- Python Pandas - IO Tools
- Python Pandas - IO Tools
- Python Pandas - Working with CSV Format
- Python Pandas - Reading & Writing JSON Files
- Python Pandas - Reading Data from an Excel File
- Python Pandas - Writing Data to Excel Files
- Python Pandas - Working with HTML Data
- Python Pandas - Clipboard
- Python Pandas - Working with HDF5 Format
- Python Pandas - Comparison with SQL
- Python Pandas - Data Handling
- Python Pandas - Sorting
- Python Pandas - Reindexing
- Python Pandas - Iteration
- Python Pandas - Concatenation
- Python Pandas - Statistical Functions
- Python Pandas - Descriptive Statistics
- Python Pandas - Working with Text Data
- Python Pandas - Function Application
- Python Pandas - Options & Customization
- Python Pandas - Window Functions
- Python Pandas - Aggregations
- Python Pandas - Merging/Joining
- Python Pandas - MultiIndex
- Python Pandas - Basics of MultiIndex
- Python Pandas - Indexing with MultiIndex
- Python Pandas - Advanced Reindexing with MultiIndex
- Python Pandas - Renaming MultiIndex Labels
- Python Pandas - Sorting a MultiIndex
- Python Pandas - Binary Operations
- Python Pandas - Binary Comparison Operations
- Python Pandas - Boolean Indexing
- Python Pandas - Boolean Masking
- Python Pandas - Data Reshaping & Pivoting
- Python Pandas - Pivoting
- Python Pandas - Stacking & Unstacking
- Python Pandas - Melting
- Python Pandas - Computing Dummy Variables
- Python Pandas - Categorical Data
- Python Pandas - Categorical Data
- Python Pandas - Ordering & Sorting Categorical Data
- Python Pandas - Comparing Categorical Data
- Python Pandas - Handling Missing Data
- Python Pandas - Missing Data
- Python Pandas - Filling Missing Data
- Python Pandas - Interpolation of Missing Values
- Python Pandas - Dropping Missing Data
- Python Pandas - Calculations with Missing Data
- Python Pandas - Handling Duplicates
- Python Pandas - Duplicated Data
- Python Pandas - Counting & Retrieving Unique Elements
- Python Pandas - Duplicated Labels
- Python Pandas - Grouping & Aggregation
- Python Pandas - GroupBy
- Python Pandas - Time-series Data
- Python Pandas - Date Functionality
- Python Pandas - Timedelta
- Python Pandas - Sparse Data Structures
- Python Pandas - Sparse Data
- Python Pandas - Visualization
- Python Pandas - Visualization
- Python Pandas - Additional Concepts
- Python Pandas - Caveats & Gotchas
Python Pandas - Modifying DataFrame
Pandas DataFrame is two-dimensional data structure that can be used for storing and manipulating tabular data. It consists of rows and columns making it similar to a spreadsheet or SQL table. Modifying a Pandas DataFrame is a crucial step in data preprocessing, data analysis, and data cleaning.
Some of the most common DataFrame modifications include −
Renaming column or row labels.
Adding or inserting new columns.
Updating or replacing existing column values.
Removing unnecessary columns.
In this tutorial, we will learn about how to modify Pandas DataFrames in different ways.
Renaming Column Labels in a DataFrame
Renaming column or row labels improves data readability and helps standardize column names. The rename() method in Pandas allows renaming one or more columns or row labels.
Example
The following example uses the DataFrame.rename() method to rename a columns name of a DataFrame.
import pandas as pd # Create a DataFrame df = pd.DataFrame({'A': [1, 2, 3],'B': [4, 5, 6]}) # Display original DataFrame print("Original DataFrame:") print(df) # Rename column 'A' to 'aa' df = df.rename(columns={'A': 'aa'}) # Display modified DataFrame print("Modified DataFrame:") print(df)
Following is the output of the above code −
Original DataFrame:
A | B | |
---|---|---|
0 | 1 | 4 |
1 | 2 | 5 |
2 | 3 | 6 |
aa | B | |
---|---|---|
0 | 1 | 4 |
1 | 2 | 5 |
2 | 3 | 6 |
Renaming Row Labels in a DataFrame
Similarly, you can rename row labels using of a Pandas DataFrame using the index parameter of the rename() method.
Example
This example demonstrates how to rename the row labels of a Pandas DataFrame using the rename(index={}) method.
import pandas as pd # Create a DataFrame df = pd.DataFrame({'A': [1, 2, 3],'B': [4, 5, 6]}, index=['x', 'y', 'z']) # Display original DataFrame print("Original DataFrame:") print(df) # Rename the multiple row labels df = df.rename(index={'x': 'r1', 'y':'r2', 'z':'r3'}) # Display modified DataFrame print("Modified DataFrame:") print(df)
Following is the output of the above code −
Original DataFrame:
A | B | |
---|---|---|
x | 1 | 4 |
y | 2 | 5 |
z | 3 | 6 |
A | B | |
---|---|---|
r1 | 1 | 4 |
r2 | 2 | 5 |
r3 | 3 | 6 |
Adding or Inserting Columns in a DataFrame
Adding a new column to an existing DataFrame is straightforward. The simplest way is to directly assign values to the DataFrame using a new column name. Additionally, you can use the DataFrame.insert() method to insert a new column at a specified location.
Example: Adding a New Column Directly
The following example demonstrates how to add a new column directly to a DataFrame.
import pandas as pd # Create a DataFrame df = pd.DataFrame({'A': [1, 2, 3],'B': [4, 5, 6]}) # Add a new column 'C' with values df['C'] = [7, 8, 9] # Display updated DataFrame print("DataFrame after adding a new column 'C':") print(df)
Following is the output of the above code −
DataFrame after adding a new column 'C':
A | B | C | |
---|---|---|---|
0 | 1 | 4 | 7 |
1 | 2 | 5 | 8 |
2 | 3 | 6 | 9 |
Example: Inserting a Column at a Specific Position
This example demonstrates how to insert a column at a specific index of a DataFrame using the DataFrame.insert() method. In this example we will insert the columns D at index position 1.
import pandas as pd # Create a DataFrame df = pd.DataFrame({'A': [1, 2, 3],'B': [4, 5, 6]}) # Insert a new column 'D' at position 1 df.insert(1, 'D', [10, 11, 12]) # Display updated DataFrame print("DataFrame after inserting column 'D' at position 1:") print(df)
Following is the output of the above code −
DataFrame after inserting column 'D' at position 1:
A | D | B | |
---|---|---|---|
0 | 1 | 10 | 4 |
1 | 2 | 11 | 5 |
2 | 3 | 12 | 6 |
Replacing the Contents of a DataFrame
Replacing the contents of the DataFrame can be done by multiple ways, one of the easiest way is assigning new values directly to the particular part of the DataFrame.
Example: Replacing a Columns Values
The following example demonstrates how to replace/update particular column values of a DataFrame using the direct assignment way.
import pandas as pd # Create a DataFrame df = pd.DataFrame({'A': [1, 2, 3],'B': [4, 5, 6]}) # Replace the contents of column 'A' with new values df['A'] = [10, 20, 30] # Display updated DataFrame print("DataFrame after replacing column 'A':") print(df)
Following is the output of the above code −
DataFrame after replacing column 'A':
A | B | |
---|---|---|
0 | 10 | 4 |
1 | 20 | 5 |
2 | 30 | 6 |
Replacing Specific Values using the replace() method
You can also use the DataFrame.replace() method to replace specific values within a column of a DataFrame.
Example
This example demonstrates how to replace specific values in a DataFrame using the DataFrame.replace() method.
import pandas as pd # Create a DataFrame df = pd.DataFrame({'A': [1, 2, 3],'B': [4, 5, 6]}) # Display the Input DataFrame print("Original DataFrame:", df, sep='\n') # Replace the contents df.replace({'A': 1, 'B': 6}, 100, inplace=True) # Display updated DataFrame print("DataFrame after replacing column 'A':") print(df)
Following is the output of the above code −
Original DataFrame:
A | B | |
---|---|---|
0 | 1 | 4 |
1 | 2 | 5 |
2 | 3 | 6 |
A | B | |
---|---|---|
0 | 100 | 4 |
1 | 2 | 5 |
2 | 3 | 100 |
Deleting Columns
Removing unnecessary columns is essential for data cleaning. You can delete single or multiple columns of a DataFrame using the DataFrame.drop() method.
Example
Here is an example that demonstrates how to delete multiple columns from a Pandas DataFrame using the DataFrame.drop() method.
import pandas as pd # Create a DataFrame df = pd.DataFrame({'A': [1, 2, 3],'B': [4, 5, 6],'C': [7, 8, 9]}) # Display the original DataFrame print("Original DataFrame:", df, sep='\n') # Delete columns 'A' and 'B' df = df.drop(columns=['A', 'B']) # Display updated DataFrame print("DataFrame after deleting columns 'A' and 'B':") print(df)
Following is the output of the above code −
Original DataFrame:
A | B | C | |
---|---|---|---|
0 | 1 | 4 | 7 |
1 | 2 | 5 | 8 |
2 | 3 | 6 | 9 |
C | |
---|---|
0 | 7 |
1 | 8 |
2 | 9 |