熊猫df。申请包括NAN值的条件

发布于 2025-02-07 02:33:14 字数 659 浏览 2 评论 0原文

我正在尝试使用应用方法和lambda函数和属性过滤的pandas.dataframe在我的pandas.dataframe中替换异常值和NAN值。我尝试了三种不同的方式,但似乎并没有捕获NAN值的案例。

df.apply(   lambda row: mode 
        if (row['prop1']>quart95 or row['prop1']==np.nan) and row['prop2']=='some_value'
        else row['prop1'], axis=1 )

df.apply(   lambda row: mode 
            if (row['prop1']>quart95 or row['prop1']==None) and row['prop2']=='some_value'
            else row['prop1'], axis=1 )

df.apply(   lambda row: mode 
            if (row['prop1']>quart95 or not row['prop1']) and row['prop2']=='some_value'
            else row['prop1'], axis=1 )

为什么要找到异常值,但与NAN一起工作? 我该如何修复或这样做?

I'm trying to replace outliers and NaN values in my pandas.DataFrame with the mode of the series, using the apply method and a lambda function and filtering by a property. I've tried in three different ways but it doesn't seems to capture the cases with NaN values.

df.apply(   lambda row: mode 
        if (row['prop1']>quart95 or row['prop1']==np.nan) and row['prop2']=='some_value'
        else row['prop1'], axis=1 )

df.apply(   lambda row: mode 
            if (row['prop1']>quart95 or row['prop1']==None) and row['prop2']=='some_value'
            else row['prop1'], axis=1 )

df.apply(   lambda row: mode 
            if (row['prop1']>quart95 or not row['prop1']) and row['prop2']=='some_value'
            else row['prop1'], axis=1 )

Why it works finding the outliers but it doesn't with the NaNs ?
How could I fix it or do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云仙小弟 2025-02-14 02:33:14

假设模式是一个值(不是列表):

df[df[column_name].isna() | df[column_name]>quart95] = mode

您可以使用bitwise或Operator(|)来执行此条件。 PLESE,请参阅此链接以了解更多有关位的操作员。

supposing mode is a single value (not a list):

df[df[column_name].isna() | df[column_name]>quart95] = mode

You can use the bitwise OR operator (|) to do this condition. Plese, refer to this link to understand more about bitwise operators.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文