Movatterモバイル変換


[0]ホーム

URL:


Python Pandas Tutorial

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. Therename() method in Pandas allows renaming one or more columns or row labels.

Example

The following example uses theDataFrame.rename() method to rename a columns name of a DataFrame.

import pandas as pd# Create a DataFramedf = pd.DataFrame({'A': [1, 2, 3],'B': [4, 5, 6]})# Display original DataFrameprint("Original DataFrame:")print(df)# Rename column 'A' to 'aa'df = df.rename(columns={'A': 'aa'})# Display modified DataFrameprint("Modified DataFrame:")print(df)

Following is the output of the above code −

Original DataFrame:
AB
014
125
236
Modified DataFrame:
aaB
014
125
236

Renaming Row Labels in a DataFrame

Similarly, you can rename row labels using of a Pandas DataFrame using theindex parameter of therename() method.

Example

This example demonstrates how to rename the row labels of a Pandas DataFrame using therename(index={}) method.

import pandas as pd# Create a DataFramedf = pd.DataFrame({'A': [1, 2, 3],'B': [4, 5, 6]}, index=['x', 'y', 'z'])# Display original DataFrameprint("Original DataFrame:")print(df)# Rename the multiple row labelsdf = df.rename(index={'x': 'r1', 'y':'r2', 'z':'r3'})# Display modified DataFrameprint("Modified DataFrame:")print(df)

Following is the output of the above code −

Original DataFrame:
AB
x14
y25
z36
Modified DataFrame:
AB
r114
r225
r336

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 theDataFrame.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 DataFramedf = pd.DataFrame({'A': [1, 2, 3],'B': [4, 5, 6]})# Add a new column 'C' with valuesdf['C'] = [7, 8, 9]# Display updated DataFrameprint("DataFrame after adding a new column 'C':")print(df)

Following is the output of the above code −

DataFrame after adding a new column 'C':
ABC
0147
1258
2369

Example: Inserting a Column at a Specific Position

This example demonstrates how to insert a column at a specific index of a DataFrame using theDataFrame.insert() method. In this example we will insert the columnsD at index position1.

import pandas as pd# Create a DataFramedf = pd.DataFrame({'A': [1, 2, 3],'B': [4, 5, 6]})# Insert a new column 'D' at position 1df.insert(1, 'D', [10, 11, 12])# Display updated DataFrameprint("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:
ADB
01104
12115
23126

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 DataFramedf = pd.DataFrame({'A': [1, 2, 3],'B': [4, 5, 6]})# Replace the contents of column 'A' with new valuesdf['A'] = [10, 20, 30]# Display updated DataFrameprint("DataFrame after replacing column 'A':")print(df)

Following is the output of the above code −

DataFrame after replacing column 'A':
AB
0104
1205
2306

Replacing Specific Values using the replace() method

You can also use theDataFrame.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 theDataFrame.replace() method.

import pandas as pd# Create a DataFramedf = 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 DataFrameprint("DataFrame after replacing column 'A':")print(df)

Following is the output of the above code −

Original DataFrame:
AB
014
125
236
DataFrame after replacing column 'A':
AB
01004
125
23100

Deleting Columns

Removing unnecessary columns is essential for data cleaning. You can delete single or multiple columns of a DataFrame using theDataFrame.drop() method.

Example

Here is an example that demonstrates how to delete multiple columns from a Pandas DataFrame using theDataFrame.drop() method.

import pandas as pd# Create a DataFramedf = pd.DataFrame({'A': [1, 2, 3],'B': [4, 5, 6],'C': [7, 8, 9]})# Display the original DataFrameprint("Original DataFrame:", df, sep='\n')# Delete columns 'A' and 'B'df = df.drop(columns=['A', 'B'])# Display updated DataFrameprint("DataFrame after deleting columns 'A' and 'B':")print(df)

Following is the output of the above code −

Original DataFrame:
ABC
0147
1258
2369
DataFrame after deleting columns 'A' and 'B':
C
07
18
29
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp