Python PANDAS 从 df 中删除一系列行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15703283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:43:25  来源:igfitidea点击:

PANDAS drop a range of rows from df

pythondataframepandas

提问by beets

I want to drop m number of rows from the bottom of a data frame. It is integer indexed (with holes). How can this be done?

我想从数据框的底部删除 m 行。它是整数索引(带孔)。如何才能做到这一点?

pandas == 0.10.1 python == 2.7.3

大熊猫== 0.10.1 蟒== 2.7.3

采纳答案by HYRY

Use slice to select the part you want:

使用 slice 选择你想要的部分:

df[:-m]

If you want to remove some middle rows, you can use drop:

如果要删除一些中间行,可以使用drop

df.drop(df.index[3:5])

回答by user1827356

This should work

这应该工作

df.head(len(df) - m)

回答by Julia Cowper

an example where the range you want to drop is indexes between x and y which I have set to 0 and 10

您要删除的范围是 x 和 y 之间的索引的示例,我已将其设置为 0 和 10

selecting just the locations between 0 and 10 to see the rows to confirm before removing

仅选择 0 到 10 之间的位置以查看要在删除之前确认的行

x=0 # could change x and y to a start and end date
y=10 
df.loc[x:y]

selecting the index

选择索引

df.loc[x:y].index

so to remove selection from dataframe

所以要从数据框中删除选择

df.drop(df.loc[x:y].index, inplace=True)