Python Pandas - 訪問 DataFrame



Pandas DataFrame 是一種二維帶標籤的資料結構,具有行和列標籤,它看起來和工作方式類似於資料庫或電子表格中的表格。為了使用 DataFrame 標籤,pandas 提供了簡單的工具來使用索引訪問和修改行和列,即 DataFrame 的indexcolumns 屬性。

在本教程中,我們將學習如何使用 DataFrame 的indexcolumns 屬性來訪問和操作 DataFrame 的行和列。

訪問 DataFrame 行

Pandas 的DataFrame.index 屬性用於訪問 DataFrame 的行標籤。它返回一個索引物件,其中包含與 DataFrame 每行中表示的資料相對應的標籤序列。這些標籤可以是整數、字串或其他可雜湊型別。

示例:訪問 DataFrame 行標籤

以下示例使用pd.index 屬性訪問 DataFrame 行標籤。

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)

以下是上述程式碼的輸出 -

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

示例:修改 DataFrame 行索引

此示例訪問並修改 Pandas DataFrame 的行標籤。

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)

執行上述程式碼後,您將獲得以下輸出 -

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

訪問 DataFrame 列

Pandas 的pd.columns 屬性用於訪問 DataFrame 中列的標籤。您可以類似於我們處理行標籤的方式來訪問和修改這些列標籤。

示例:訪問 DataFrame 列標籤

以下示例使用pd.columns 屬性訪問 DataFrame 列標籤。

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)

以下是上述程式碼的輸出 -

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

示例:修改 DataFrame 列標籤

此示例使用pd.columns 屬性訪問並修改 DataFrame 列標籤。

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)

以下是上述程式碼的輸出 -

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
廣告