在 Pandas 和 Numpy Python 中保留 Nan 的同时进行值比较的更优雅的方法 [英] More elegant way to do value comparison while preserving Nan in Pandas and Numpy Python

查看:59
本文介绍了在 Pandas 和 Numpy Python 中保留 Nan 的同时进行值比较的更优雅的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以基本上我想要 5 >np.nan 返回 np.nan 或 Nan 而不是 FALSE
在熊猫系列中,代码如下:

So basically I want 5 > np.nan return np.nan or Nan instead of FALSE
In pandas series, here's the code :

import pandas as pd
import numpy as np
a = pd.DataFrame({"x":[1,2,3,4],"y":[1,np.nan,5,1]})

a["x"]>a["y"]

将返回:

0    False
1    False
2    False
3    True
dtype: bool

我目前保存 Nan 信息的方法是:

My current approach to preserve the Nan information is :

value_comparison = a["x"]>a["y"]
nan_comparison = a["x"].isna() | a["y"].isna()
value_comparison.where(~nan_comparison,np.nan)

返回的地方

0    0.0
1    NaN
2    0.0
3    1.0
dtype: float64

我也采用了类似的方法进行 numpy 比较

I took the similar approach for numpy comparison too

即使结果是正确的,我相信我的解决方案不优雅,有没有更好的(pandas 和 numpy)方法来做到这一点,遵循 巨蟒之禅 ?(更好的可读性,更直接)

Even when the result is correct, I believe my solution is not elegant, is there any better(pandas and numpy) way to do this, which follows the zen of python ? (better readability, more straighforward)

推荐答案

仅稍微改进/(更改)您的解决方案:

Only a bit improved/(changed) your solution:

value_comparison = (a["x"]>a["y"])
nan_comparison = a[["x", "y"]].notna().all(axis=1)
#alternative
#nan_comparison = a["x"].notna() & a["y"].notna()
m = value_comparison.where(nan_comparison)
print (m)
0    0.0
1    NaN
2    0.0
3    1.0
dtype: float64

最后可以转换为可空布尔值:

Last is possible convert to nullable boolean:

m = value_comparison.where(nan_comparison).astype('boolean')
print (m)
0    False
1     <NA>
2    False
3     True
dtype: boolean

这篇关于在 Pandas 和 Numpy Python 中保留 Nan 的同时进行值比较的更优雅的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆