Movatterモバイル変換


[0]ホーム

URL:


Python Pandas Tutorial

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 theindex andcolumns attributes of a DataFrame.

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

Accessing the DataFrame Rows Labels

Theindex 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 thepd.index attribute.

import pandas as pd# Create a DataFramedf = 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 DataFrameresult = df.indexprint('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 theindex 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 theindex attribute.

import pandas as pd# Create a DataFramedf = 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 DataFrameprint('Input DataFrame:\n', df)# Modify the Row labels of the DataFramedf.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:
NameAgeGenderRating
r1Steve32Male3.45
r2Lia28Female4.60
r3Vin45Male3.90
r4Katie38Female2.78
Output Modified DataFrame with the updated index labels:
NameAgeGenderRating
100Steve32Male3.45
200Lia28Female4.60
300Vin45Male3.90
400Katie38Female2.78

Accessing The DataFrame Columns Labels

The Pandaspd.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 thepd.columns attribute.

import pandas as pd# Create a DataFramedf = 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 DataFrameresult = df.columnsprint('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 thecolumns attribute.

Example

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

import pandas as pd# Create a DataFramedf = 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 DataFrameprint('Input DataFrame:\n', df)# Modify the Column labels of the DataFramedf.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:
NameAgeGenderRating
r1Steve32Male3.45
r2Lia28Female4.60
r3Vin45Male3.90
r4Katie38Female2.78
Output Modified DataFrame with the updated Column Labels
Col1Col2Col3Col4
r1Steve32Male3.45
r2Lia28Female4.60
r3Vin45Male3.90
r4Katie38Female2.78
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp