4 Pandas Functions for Element-Wise Comparison of DataFrames | by Soner Yıldırım | Jun, 2023


1. combine

The combine function does an element-wise comparison based on the given function.

For instance, we can select the maximum value out of two values for each position. It’ll be more clear when we do the example.

combined_df = df1.combine(df2, np.maximum)
(image by author)

Take a look at the value in the first row and first column. The combined DataFrame has the bigger one of 5 and 2.

If one of the values is NaN (i.e. missing value), the combined DataFrame at this position has NaN as well because Pandas can’t compare a value with a missing value.

We can choose a constant value to be used in the case of missing values by using the fill_value parameter. Missing values are filled with this value before comparing them to the values in the other DataFrame.

combined_df = df1.combine(df2, np.maximum, fill_value=0)
(image by author)

There are two NaN values in df1, which are filled with 0 and then compared to the values in the same position of df2.



Source link

Leave a Comment