Python Pandas - Accessing DataFrame



Pandas DataFrame is a two-dimensional labeled data structure with rows and columns labels, it is looks and works similar to a table in a database or a spreadsheet. To work with the DataFrame labels, pandas provides simple tools to access and modify the rows and columns using index the index and columns attributes of a DataFrame.

In this tutorial, we will learn about how to access and modify rows and columns in a Pandas DataFrame using the index and columns attributes of the DataFrame.

Accessing the DataFrame Rows Labels

The index attribute in Pandas is used to access row labels in a DataFrame. It returns an index object containing the series of labels corresponding to the data represented in the each row of the DataFrame. These labels can be integers, strings, or other hashable types.

Example

The following example access the DataFrame row labels using the pd.index attribute.

Open Compiler
import pandas as pd # Create a DataFrame df = pd.DataFrame({ 'Name': ['Steve', 'Lia', 'Vin', 'Katie'], 'Age': [32, 28, 45, 38], 'Gender': ['Male', 'Female', 'Male', 'Female'], 'Rating': [3.45, 4.6, 3.9, 2.78]}, index=['r1', 'r2', 'r3', 'r4']) # Access the rows of the DataFrame result = df.index print('Output Accessed Row Labels:', result)

Following is the output of the above code −

Output Accessed Row Labels: Index(['r1', 'r2', 'r3', 'r4'], dtype='object')

Modifying DataFrame Row Labels

With the index attribute you can also modify the row labels of a DataFrame.

Example

Here is an example that demonstrates accessing and modifying the row labels of the Pandas DataFrame using the index attribute.

Open Compiler
import pandas as pd # Create a DataFrame df = pd.DataFrame({ 'Name': ['Steve', 'Lia', 'Vin', 'Katie'], 'Age': [32, 28, 45, 38], 'Gender': ['Male', 'Female', 'Male', 'Female'], 'Rating': [3.45, 4.6, 3.9, 2.78]}, index=['r1', 'r2', 'r3', 'r4']) # Display the Input DataFrame print('Input DataFrame:\n', df) # Modify the Row labels of the DataFrame df.index = [100, 200, 300, 400] print('Output Modified DataFrame with the updated index labels:\n', df)

On executing the above code you will get the following output −

Input DataFrame:
Name Age Gender Rating
r1 Steve 32 Male 3.45
r2 Lia 28 Female 4.60
r3 Vin 45 Male 3.90
r4 Katie 38 Female 2.78
Output Modified DataFrame with the updated index labels:
Name Age Gender Rating
100 Steve 32 Male 3.45
200 Lia 28 Female 4.60
300 Vin 45 Male 3.90
400 Katie 38 Female 2.78

Accessing The DataFrame Columns Labels

The Pandas pd.columns attribute is used to access the labels of the columns in the DataFrame. You can access and modify these column labels similarly to how we work with row labels.

Example

The following example demonstrates how to access the DataFrame column labels using the pd.columns attribute.

Open Compiler
import pandas as pd # Create a DataFrame df = pd.DataFrame({ 'Name': ['Steve', 'Lia', 'Vin', 'Katie'], 'Age': [32, 28, 45, 38], 'Gender': ['Male', 'Female', 'Male', 'Female'], 'Rating': [3.45, 4.6, 3.9, 2.78]}, index=['r1', 'r2', 'r3', 'r4']) # Access the column labels of the DataFrame result = df.columns print('Output Accessed column Labels:', result)

Following is the output of the above code −

Output Accessed column Labels: Index(['Name', 'Age', 'Gender', 'Rating'], dtype='object')

Modifying the DataFrame Column Labels

Column labels can be modified using the columns attribute.

Example

This example demonstrates how to access and modify the DataFrame column labels using the pd.columns attribute.

Open Compiler
import pandas as pd # Create a DataFrame df = pd.DataFrame({ 'Name': ['Steve', 'Lia', 'Vin', 'Katie'], 'Age': [32, 28, 45, 38], 'Gender': ['Male', 'Female', 'Male', 'Female'], 'Rating': [3.45, 4.6, 3.9, 2.78]}, index=['r1', 'r2', 'r3', 'r4']) # Display the Input DataFrame print('Input DataFrame:\n', df) # Modify the Column labels of the DataFrame df.columns = ['Col1', 'Col2', 'Col3', 'Col4'] print('Output Modified DataFrame with the updated Column Labels\n:', df)

Following is the output of the above code −

Input DataFrame:
Name Age Gender Rating
r1 Steve 32 Male 3.45
r2 Lia 28 Female 4.60
r3 Vin 45 Male 3.90
r4 Katie 38 Female 2.78
Output Modified DataFrame with the updated Column Labels
Col1 Col2 Col3 Col4
r1 Steve 32 Male 3.45
r2 Lia 28 Female 4.60
r3 Vin 45 Male 3.90
r4 Katie 38 Female 2.78
Advertisements