我上传了我的文件 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/