如何使用 iloc[] 选择 pandas 数据帧的倒数第二行?

4 python row dataframe python-3.x pandas

我从网上获取数据并将该数据存储在 pandas 数据框中。但问题是数据帧格式每次都不相同,主要是行数。

\n
Print(df.shape)\nOutput: (100, 9)\n
Run Code Online (Sandbox Code Playgroud)\n

--

\n
Print(df.shape)\nOutput: (33, 9)\n
Run Code Online (Sandbox Code Playgroud)\n

--

\n
Print(df.shape)\nOutput: (153, 9)\n
Run Code Online (Sandbox Code Playgroud)\n

--

\n
Print(df.shape)\nOutput: (148, 9)\n
Run Code Online (Sandbox Code Playgroud)\n

您能否告诉我如何使用 iloc[] 仅选择倒数第二行或倒数第二行\xe2\x80\x99s 任何特定单元格

\n

小智 8

df.iloc[-2]将为您提供所有列的倒数第二行信息。

如果您只想要特定的列,df.loc不喜欢减号,那么您可以这样做的一种方法是:df.loc[(df.shape[0]-2), 'your_column_name']

wheredf.shape[0]获取行数,-2 从中删除 2,为您提供倒数第二行的索引号。然后,您为其提供所需的列名称以仅返回该值。