获取 dataframe pandas 的第一行-ag捕鱼王app官网

获取 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

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便
网站地图