2

I have 2 dataframes:

df1:
   a  b   c
0  1  2   6
1  2  3   7
2  3  4   8
3  4  5   9
4  5  6  10

df2:
     a    b     c
0  NaN  NaN   NaN
1  1.0  NaN   NaN
2  2.0  2.0   NaN
3  4.0  3.0   NaN
4  6.0  6.0  11.0

When I trying to do df1 > df2, the output is:

In [150]:df1 > df2
Out[150]: 
       a      b      c
0  False  False  False
1   True  False  False
2   True   True  False
3  False   True  False
4  False  False  False

But what I expect is like this:

       a      b      c
0    NaN    NaN    NaN
1   True    NaN    NaN
2   True   True    NaN
3  False   True    NaN
4  False  False  False

So, how should I do to compare 2 df and keep the null to null?

Tan Tu
  • 21
  • 2

1 Answers1

2

Let's try:

df1.gt(df2).astype(str).mask(df2.isnull())

Output:

       a      b      c
0    NaN    NaN    NaN
1   True    NaN    NaN
2   True   True    NaN
3  False   True    NaN
4  False  False  False

You could try, but the way pandas changes the dtype of any series with a null to float dtype you will get the following:

df1.gt(df2).mask(df2.isnull())

Output:

     a    b    c
0  NaN  NaN  NaN
1  1.0  NaN  NaN
2  1.0  1.0  NaN
3  0.0  1.0  NaN
4  0.0  0.0  0.0
Scott Boston
  • 147,308
  • 15
  • 139
  • 187