首页 > 解决方案 > 从数据框中删除特定行

问题描述

我想从一个数据框中删除特定行

数据框有176039行,我想在数据框2%的开头和2%结尾删除它们

数据框如下

    activity    accx    accy    accz    gyrx    gyry    gyrz
  0 downstairs  0.660583    0.454468    -0.585022   32.366615   27.206556    
  -23.471800
  1 downstairs  0.668640    0.454102    -0.577698   32.442837   27.168446    
 -24.679878
  2 downstairs  0.672241    0.453613    -0.574158   33.647106   26.280489    
 -25.243902
  3 downstairs  0.688599    0.454041    -0.574768   34.676067   25.076220    
 -26.204270
  4 downstairs  0.692017    0.443604    -0.559814   34.855183   25.003810    
 -27.317074

有任何想法吗?

标签: pythonpandasdataframe

解决方案


您可以使用iloc和获取(到最近的条目)2%98%沿途的索引。

num = len(df)
df = df.iloc[int(0.02 * num):int(0.98 * num)]

根据您的修正案,您可以通过以下方式确保您的数据框具有 200 行的倍数:

df = df.iloc[:-(len(df) % 200)]

推荐阅读