如何删除 pandas 数据框的最后一行数据 [英] How to delete the last row of data of a pandas dataframe

查看:122
本文介绍了如何删除 pandas 数据框的最后一行数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这应该很简单,但是我尝试了一些想法,但没有一个起作用:

I think this should be simple, but I tried a few ideas and none of them worked:

last_row = len(DF)
DF = DF.drop(DF.index[last_row])  #<-- fail!

我尝试使用负索引,但这也会导致错误.我仍然会误解一些基本知识.

I tried using negative indices but that also lead to errors. I must still be misunderstanding something basic.

推荐答案

要删除最后n行:

df.drop(df.tail(n).index,inplace=True) # drop last n rows

通过同样的方法,您可以删除前n行:

By the same vein, you can drop first n rows:

df.drop(df.head(n).index,inplace=True) # drop first n rows

这篇关于如何删除 pandas 数据框的最后一行数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆