python - 按特定值选择行时出现棘手错误

标签 python python-2.7 pandas

我上传了我的文件 here并希望通过某些特征的值选择某些样本(行)。

这是我的尝试:

df[df['Sample_ID'] == '160606-6']['OC_unc'].values
>array([ 0.9874218,  1.089288 ])
 # just locate to the sample with ["OC_unc] == 0.9874218
df.loc[df['OC_unc'] == 0.9874218]
> No result

我不知道为什么这个方法对 float 据失败。我尝试使用字符串列表选择行,它总是运行良好

最佳答案

我怀疑这与 pandas 中的浮点截断有关。考虑以下示例:

>>> df = pd.DataFrame([1.00000000001], columns=['test'])
>>> df
   test
0   1.0
>>> df.loc[df['test'] == 1.0]
Empty DataFrame
Columns: [test]
Index: []

解决此问题的一种方法是使用 pd.set_option 提高显示精度:

>>> pd.set_option('precision', 15)
>>> df
            test
0  1.00000000001
>>> df.loc[df['test'] == 1.00000000001]
            test
0  1.00000000001

关于python - 按特定值选择行时出现棘手错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40182381/

相关文章:

python - pandas groupby 长度与 NaN 不匹配

Python 请求 ImportError : cannot import name HeaderParsingError

python - pandas dataframe的高效读写

python - 是否可以在不安装浏览器的情况下使用 python 抓取网站?

python - 存储过程获取最后插入的行python mysql连接器

python-2.7 - 操作系统错误 : [Errno 40] Too many levels of symbolic links: '/home/ahmed/.virtualenvs/cv/bin/python'

Python ctypes - 当字符串嵌入空值时设置 c_char 数组?

python - 查找特定的顺序模式

python - 将包添加到 PyPi 时发生 SSL 错误

Python 和奇怪的套接字行为