在 Python 中,您可以使用以下方法来获取 Pandas DataFrame 中的最后一行:
- 使用
tail
方法:
last_row = df.tail(1)
- 使用索引:
last_row = df.iloc[-1]
- 使用
loc
方法:
last_row = df.loc[df.index[-1]]
- 使用
sort_index
方法和索引:
df = df.sort_index()
last_row = df.iloc[-1]
请注意,这些方法都返回一个新的 DataFrame,其中包含单个行。如果您想要获取单个值,可以使用索引或属性访问。例如,若要获取最后一行的第一列,可以使用:
last_row_first_column = df.tail(1).iloc[0, 0]
或
last_row_first_column = df.tail(1).loc[df.index[-1], df.columns[0]]