DataFrame 獲取給定列的第一行

Suraj Joshi 2023年1月30日 2021年2月28日
  1. 使用 Series.loc() 獲取 DataFrame 中特定列的第一行
  2. 使用 Series.loc() 獲取 DataFrame 中特定列的第一行
DataFrame 獲取給定列的第一行

本教程介紹瞭如何使用 Series.loc()Series.iloc() 方法獲得 DataFrame 中給定列的第一行。

我們將在本文中使用下面的 DataFrame 示例。

import pandas as pd

roll_no = [501, 502, 503, 504, 505]

student_df = pd.DataFrame({
    'Name': ["Jennifer", "Travis", "Bob", "Emma", "Luna", "Anish"],
    'Gender':  ["Female", "Male", "Male", "Female", "Female", "Male"],
    'Age': [17, 18, 17, 16, 18, 16]
},index=roll_no)

print(student_df)

輸出:

         Name Gender Age
501 Jennifer Female   17
502    Travis    Male   18
503       Bob    Male   17
504      Emma Female   16
505      Luna Female   18
506     Anish    Male   16

使用 Series.loc() 獲取 DataFrame 中特定列的第一行

要使用 Series.loc() 從 Series 物件中獲取某一行,我們只需將該行的索引名稱作為引數傳遞給 Series.loc() 方法。

DataFrame 的每一列都是一個 Series 物件,我們可以使用 .loc() 方法來選擇給定列的任何元素。

import pandas as pd

roll_no = [501, 502, 503, 504, 505, 506]

student_df = pd.DataFrame({
    'Name': ["Jennifer", "Travis", "Bob", "Emma", "Luna", "Anish"],
    'Gender':  ["Female", "Male", "Male", "Female", "Female", "Male"],
    'Age': [17, 18, 17, 16, 18, 16]
}, index=roll_no)

print("The DataFrame is:")
print(student_df, "\n")

first_row = student_df["Name"].loc[501]

print("First row from Name column is:")
print(first_row)

輸出:

The DataFrame is:
         Name Gender Age
501 Jennifer Female   17
502    Travis    Male   18
503       Bob    Male   17
504      Emma Female   16
505      Luna Female   18
506     Anish    Male   16

First row from Name column is:
Jennifer

它從 DataFrame student_dfName 列中選擇第一行並列印出來。我們傳遞第一行的索引,即 501 來選擇第一行。

另外,我們也可以將第一行的索引和指定的列名作為引數傳遞給 loc() 方法,以提取 DataFrame 中指定列的第一行的元素。

import pandas as pd

roll_no = [501, 502, 503, 504, 505, 506]

student_df = pd.DataFrame({
    'Name': ["Jennifer", "Travis", "Bob", "Emma", "Luna", "Anish"],
    'Gender':  ["Female", "Male", "Male", "Female", "Female", "Male"],
    'Age': [17, 18, 17, 16, 18, 16]
}, index=roll_no)

print("The DataFrame is:")
print(student_df, "\n")

first_name = student_df.loc[501, 'Name']

print("First row from Name column is:")
print(first_name)

輸出:

The DataFrame is:
         Name Gender Age
501 Jennifer Female   17
502    Travis    Male   18
503       Bob    Male   17
504      Emma Female   16
505      Luna Female   18
506     Anish    Male   16

First row from Name column is:
Jennifer

它從 Name 列和第一行選擇索引值 503 的值。

使用 Series.loc() 獲取 DataFrame 中特定列的第一行

要使用 Series.iloc() 從 DataFrame 中獲取特定行,我們將該行的整數索引作為引數傳遞給 Series.iloc() 方法。

import pandas as pd

roll_no = [501, 502, 503, 504, 505, 506]

student_df = pd.DataFrame({
    'Name': ["Jennifer", "Travis", "Bob", "Emma", "Luna", "Anish"],
    'Gender':  ["Female", "Male", "Male", "Female", "Female", "Male"],
    'Age': [17, 18, 17, 16, 18, 16]
}, index=roll_no)

print("The DataFrame is:")
print(student_df, "\n")

first_row = student_df["Name"].iloc[0]

print("First row from Name column is:")
print(first_row)

輸出:

The DataFrame is:
         Name Gender Age
501 Jennifer Female   17
502    Travis    Male   18
503       Bob    Male   17
504      Emma Female   16
505      Luna Female   18
506     Anish    Male   16

First row from Name column is:
Jennifer

它從 DataFrame student_dfName 列中選擇第一行並列印出來。我們傳遞第一行的整數索引,即 0,因為索引從 0 開始。

另外,我們也可以將第一行的整數索引和指定列的索引同時傳遞給 iloc() 方法作為引數,以提取 DataFrame 中指定列的第一行的條目。

import pandas as pd

roll_no = [501, 502, 503, 504, 505, 506]

student_df = pd.DataFrame({
    'Name': ["Jennifer", "Travis", "Bob", "Emma", "Luna", "Anish"],
    'Gender':  ["Female", "Male", "Male", "Female", "Female", "Male"],
    'Age': [17, 18, 17, 16, 18, 16]
}, index=roll_no)

print("The DataFrame is:")
print(student_df, "\n")

first_name = student_df.iloc[0, 0]

print("Name of student at first row is:")
print(first_name)

輸出:

The DataFrame is:
         Name Gender Age
501 Jennifer Female   17
502    Travis    Male   18
503       Bob    Male   17
504      Emma Female   16
505      Luna Female   18
506     Anish    Male   16

Name of student at first row is:
Jennifer

它從 DataFrame 的第一行和第一列中選擇數值。

Author: Suraj Joshi
Suraj Joshi avatar Suraj Joshi avatar

Suraj Joshi is a backend software engineer at Matrice.ai.

LinkedIn

相關文章 - Pandas Row

相關文章 - Pandas DataFrame Column