PANDAS从df中删除一系列行

bee*_*ets 9 python dataframe pandas

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

pandas == 0.10.1 python == 2.7.3

HYR*_*YRY 20

使用切片选择所需的零件:

df[:-m]
Run Code Online (Sandbox Code Playgroud)

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

df.drop(df.index[3:5])
Run Code Online (Sandbox Code Playgroud)


小智 6

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

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

x=0 # could change x and y to a start and end date
y=10 
df.loc[x:y]
Run Code Online (Sandbox Code Playgroud)

选择索引

df.loc[x:y].index
Run Code Online (Sandbox Code Playgroud)

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

df.drop(df.loc[x:y].index, inplace=True)
Run Code Online (Sandbox Code Playgroud)