如何使 NaN 数值低于任何其他数值 value.during 比较?

How do I make NaN numeric values to be lower than any other numeric value.during comparisons?

底线,我希望在比较中,NaN 数值低于任何其他数值。

假设我有 s1 和 s2,

s1 = pd.Series([1, 3, np.nan, 5, np.nan, -1, np.nan])
s2 = pd.Series([2, 1, np.nan, np.nan, 2, np.nan, -1])

当我将它们比较为 s1 < s2 时,我想要以下行为:

Out: 
0    True
1    False
2    False
3    False
4    True
5    False
6    True

使用 or 条件修复 s1 为空而 s2 不为空的行。

(s1 < s2) | (s1.isnull() & s2.notnull())
#0     True
#1    False
#2    False
#3    False
#4     True
#5    False
#6     True
#dtype: bool

只需使用 Series.fillna function and np.NINF 常量:

In [256]: s2.fillna(np.NINF) > s1.fillna(np.NINF)                                                                                              
Out[256]: 
0     True
1    False
2    False
3    False
4     True
5    False
6     True
dtype: bool
  • np.NINF - NumPy 常量,负无穷大的浮点表示