Skip to content Skip to sidebar Skip to footer

How To Treat Nan Or Non Aligned Values As 1s Or 0s In Multiplying Pandas Dataframes

I want to treat non aligned or missing (NaN, Inf, -Inf) values as 1s or 0s. df1 = pd.DataFrame({'x':[1, 2, 3, 4, 5], 'y':[3, 4, 5, 6, 7]}, index=['a', 'b', 'c', 'd', 'e']

Solution 1:

I think you need DataFrame.mul with fillna or combine_first in solution 1 and 2:

print (df1.mul(df2).fillna(df1))
     x     y   z
a  1.03.0NaN
b  2.04.0NaNc3.05.0NaN
d  4.018.0NaN
e  5.028.0NaN
f  NaNNaNNaN

print (df1.mul(df2).combine_first(df1))
     x     y   z
a  1.03.0NaN
b  2.04.0NaNc3.05.0NaN
d  4.018.0NaN
e  5.028.0NaN
f  NaNNaNNaN

print (df1.mul(df2).fillna(df2))
    x     y    z
a NaNNaNNaN
b NaN4.03.0cNaNNaN4.0
d NaN18.05.0
e NaN28.06.0
f NaN5.07.0

print (df1.mul(df2).combine_first(df2))
    x     y    z
a NaNNaNNaN
b NaN4.03.0cNaNNaN4.0
d NaN18.05.0
e NaN28.06.0
f NaN5.07.0

Solution with fill_value=1 in DataFrame.mul for 3 output:

print (df1.mul(df2, fill_value=1))
     x     y    z
a  1.03.0NaN
b  2.04.03.0c3.05.04.0
d  4.018.05.0
e  5.028.06.0
f  NaN5.07.0

Solution 2:

Case 1 Replace missing or misaligned value in df1 with 1

>>> df1.reindex(index=df1.index.union(df2.index), 
                columns=df1.columns.union(df2.columns)).fillna(1)
   xyza131b241c351d461e571f111

Append the snippet above with .mul(df2) if desired.

Case 2 Replace missing or misaligned value in df2 with 1

>>> df2.reindex(index=df2.index.union(df1.index), 
                columns=df2.columns.union(df1.columns)).fillna(1)
   xyza111b113c114d135e146f157

Append the snippet above with .mul(df1) if desired.

Case 3 Replace any missing or misaligned value with 1 if there is a value in the other DF.

>>> df1.mul(df2).combine_first(df1).combine_first(df2)
    x   y   z
a   13NaN
b   243c354
d   4185
e   5286
f NaN57

Post a Comment for "How To Treat Nan Or Non Aligned Values As 1s Or 0s In Multiplying Pandas Dataframes"