迹忆客 专注技术分享

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

获取 Dataframe Pandas 的第一行

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

本教程介绍了如何使用 pandas.DataFrame.iloc 属性和 pandas.DataFrame.head() 方法从 Pandas DataFrame 中获取第一行。

我们将在下面的例子中使用以下 DataFrame 来解释如何从 Pandas DataFrame 中获取第一行。

import pandas as pd


df = pd.DataFrame(
    {
        "C_1": ["A", "B", "C", "D"],
        "C_2": [40, 34, 38, 45],
        "C_3": [430, 980, 200, 350],
    }
)

print(df)

输出:

  C_1  C_2  C_3
0   A   40  430
1   B   34  980
2   C   38  200
3   D   45  350

使用 pandas.DataFrame.iloc 属性获取 Pandas DataFrame 的第一行

import pandas as pd


df = pd.DataFrame(
    {
        "C_1": ["A", "B", "C", "D"],
        "C_2": [40, 34, 38, 45],
        "C_3": [430, 980, 200, 350],
    }
)

row_1 = df.iloc[0]

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

print("The First Row of the DataFrame is:")
print(row_1)

输出:

The DataFrame is:
  C_1  C_2  C_3
0   A   40  430
1   B   34  980
2   C   38  200
3   D   45  350

The First Row of the DataFrame is:
C_1      A
C_2     40
C_3    430
Name: 0, dtype: object

它显示 DataFrame df 的第一行。为了选择第一行,我们使用第一行的默认索引,即 0 和 DataFrame 的 iloc 属性。

使用 pandas.DataFrame.head() 方法从 Pandas DataFrame 中获取第一行

pandas.DataFrame.head() 方法返回一个 DataFrame,其中包含 DataFrame 中最上面的 5 行。我们也可以传递一个数字作为参数给 pandas.DataFrame.head() 方法,代表要选择的最上面的行数。我们可以传递 1 作为参数到 pandas.DataFrame.head() 方法中,只选择 DataFrame 的第一行。

import pandas as pd


df = pd.DataFrame(
    {
        "C_1": ["A", "B", "C", "D"],
        "C_2": [40, 34, 38, 45],
        "C_3": [430, 980, 200, 350],
    }
)

row_1 = df.head(1)

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

print("The First Row of the DataFrame is:")
print(row_1)

输出:

The DataFrame is:
  C_1  C_2  C_3
0   A   40  430
1   B   34  980
2   C   38  200
3   D   45  350

The First Row of the DataFrame is:
  C_1  C_2  C_3
0   A   40  430

根据指定的条件从 Pandas DataFrame 中获取第一行

为了从 DataFrame 中提取满足指定条件的第一行,我们首先过滤满足指定条件的行,然后使用上面讨论的方法从过滤后的 DataFrame 中选择第一行。

import pandas as pd


df = pd.DataFrame(
    {
        "C_1": ["A", "B", "C", "D"],
        "C_2": [40, 34, 38, 45],
        "C_3": [430, 980, 500, 350],
    }
)

filtered_df = df[(df.C_2 < 40) & (df.C_3 > 450)]

row_1_filtered = filtered_df.head(1)

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

print("The Filtered DataFrame is:")
print(filtered_df, "\n")


print("The First Row with C_2 less than 45 and C_3 greater than 450 is:")
print(row_1_filtered)

输出:

The DataFrame is:
  C_1  C_2  C_3
0   A   40  430
1   B   34  980
2   C   38  500
3   D   45  350

The Filtered DataFrame is:
  C_1  C_2  C_3
1   B   34  980
2   C   38  500

The First Row with C_2 less than 45 and C_3 greater than 450 is:
  C_1  C_2  C_3
1   B   34  980

它将显示第一条列 C_2 值小于 45 且 C_3 列值大于 450 的行。

我们也可以使用 query() 方法来过滤 DataFrame 中的行。

import pandas as pd


df = pd.DataFrame(
    {
        "C_1": ["A", "B", "C", "D"],
        "C_2": [40, 34, 38, 45],
        "C_3": [430, 980, 500, 350],
    }
)

filtered_df = df.query("(C_2 < 40) & (C_3 > 450)")

row_1_filtered = filtered_df.head(1)

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

print("The Filtered DataFrame is:")
print(filtered_df, "\n")


print("The First Row with C_2 less than 45 and C_3 greater than 450 is:")
print(row_1_filtered)

输出:

The DataFrame is:
  C_1  C_2  C_3
0   A   40  430
1   B   34  980
2   C   38  500
3   D   45  350

The Filtered DataFrame is:
  C_1  C_2  C_3
1   B   34  980
2   C   38  500

The First Row with C_2 less than 45 and C_3 greater than 450 is:
  C_1  C_2  C_3
1   B   34  980

它将使用 query() 方法过滤所有列 C_2 值小于 45 和列 C_3 值大于 450 的行,然后使用 head() 方法从 filtered_df 中选择第一行。

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

本文地址:

相关文章

Pandas read_csv()函数

发布时间:2024/04/24 浏览次数:254 分类:Python

Pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 DataFrame 中。

Pandas 追加数据到 CSV 中

发布时间:2024/04/24 浏览次数:352 分类:Python

本教程演示了如何在追加模式下使用 to_csv()向现有的 CSV 文件添加数据。

Pandas 多列合并

发布时间:2024/04/24 浏览次数:628 分类:Python

本教程介绍了如何在 Pandas 中使用 DataFrame.merge()方法合并两个 DataFrames。

Pandas loc vs iloc

发布时间:2024/04/24 浏览次数:837 分类:Python

本教程介绍了如何使用 Python 中的 loc 和 iloc 从 Pandas DataFrame 中过滤数据。

扫一扫阅读全部技术教程

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

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便