python - 从数据框中删除特定行
问题描述
我想从一个数据框中删除特定行
数据框有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
有任何想法吗?
解决方案
您可以使用iloc
和获取(到最近的条目)2%
和98%
沿途的索引。
num = len(df)
df = df.iloc[int(0.02 * num):int(0.98 * num)]
根据您的修正案,您可以通过以下方式确保您的数据框具有 200 行的倍数:
df = df.iloc[:-(len(df) % 200)]
推荐阅读
- python - Python/Pandas - 如何重命名 DataFrame 中的列标题,而不会丢失列标题中的现有数据?
- kubernetes - Kubernetes 中的 Redis - 连接被拒绝
- sql - tsql 命令优化
- r - r - 按年份或星期几计算间隔小时数
- jmeter - JMeter HTTP 原始请求错误 400 错误请求
- java - Google Play 控制台警告应用正在使用不受支持的 API
- java - 你如何传递一个列表
如果 Placeholder 已经是一个接口,则到一个包 - python - 使用python从HTML表格的行中提取文本
- sql - 根据 groupBy 的结果选择 pyspark 数据帧行
- sql - 具有多个条件的 SQL 查询 2020 年 3 月 18 日