迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Python >

DataFrame 获取给定列的第一行

作者:迹忆客 最近更新:2024/04/22 浏览次数:

本教程介绍了如何使用 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 的第一行和第一列中选择数值。

上一篇:Pandas 重命名多个列

下一篇:没有了

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Pandas 重命名多个列

发布时间:2024/04/22 浏览次数:186 分类:Python

本教程演示了如何使用 Pandas 重命名数据框中的多个列。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便