简体   繁体   English

根据 isclose() 条件从数据框中选择行

[英]Select rows from dataframe based on isclose() condition

I have a dataframe orig_df with "Alpha" and "Method" column names.我有一个带有“Alpha”和“Method”列名称的数据orig_df

num is a floating point number with some floating point error. num是一个带有一些浮点错误的浮点数。 For example, it is 1.1400000000000001.例如,它是 1.1400000000000001。

Meanwhile, "Alpha" values are truncated to 1.14.同时,“Alpha”值被截断为 1.14。

As a result, I want to return all the rows such that orig_df["Alpha"] and num are close (and that method matches some string method_type , but that's less relevant).因此,我想返回所有行,使得orig_df["Alpha"]num接近(并且该method匹配某些字符串method_type ,但这不太相关)。

So far I have:到目前为止,我有:

temp_df = orig_df[
                (math.isclose(orig_df["Alpha"], num))
                & (orig_df["Method"] == method_type)
            ]

But then i receive the error但后来我收到错误

TypeError: cannot convert the series to <class 'float'>

How can I fix this?我怎样才能解决这个问题?

math.isclose is not a function that can cooperate with pandas and numpy arrays. math.isclose不是一个可以与pandasnumpy数组配合的函数。

You need numpy.isclose :你需要numpy.isclose

temp_df = orig_df[
                (np.isclose(orig_df["Alpha"], num))
                & (orig_df["Method"] == method_type)
            ]

np is numpy in this case.在这种情况下npnumpy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM