You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取DataFrame中quantity列含非.00小数的行索引

找出DataFrame中quantity列含非.00小数的行索引

针对你的需求,这里提供两种简洁的方法来提取目标行的索引:

方法1:利用取余运算判断小数部分

通过对quantity列的数值取余1,若结果不等于0,说明该数值存在非.00的小数部分。

import pandas as pd

data = {'quantity':  [2.00, 1.00, 3.00, 4.55, 5.00, 6.22]}
testdf = pd.DataFrame(data)

# 筛选并提取索引
target_indices = testdf[testdf['quantity'].mod(1) != 0].index.tolist()
print(target_indices)  # 输出: [3, 5]

方法2:通过类型转换对比判断

将quantity列的数值转换为整数后与原数值对比,若不相等则说明存在非.00的小数部分。

import pandas as pd

data = {'quantity':  [2.00, 1.00, 3.00, 4.55, 5.00, 6.22]}
testdf = pd.DataFrame(data)

# 筛选并提取索引
target_indices = testdf[testdf['quantity'] != testdf['quantity'].astype(int)].index.tolist()
print(target_indices)  # 输出: [3, 5]

两种方法都能准确筛选出你需要的行索引,可根据个人习惯选择使用。

内容的提问来源于stack exchange,提问作者tailsrockc

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 06:35:48