Creating an element-wise minimum Series from two other Series in Python Pandas

前端 未结 8 1525
南方客
南方客 2020-12-29 18:15

I am having trouble finding a way to do an efficient element-wise minimum of two Series objects in pandas. For example I can add two Series easily enough:

In         


        
8条回答
  •  离开以前
    2020-12-29 18:37

    You can use the combine method of a DataFrame with np.minimum as the argument. np.minimum has special handling for NaN and complex NaNs.

    Indeed, the pandas docs for combine uses the np.minimum function to illustrate a "true element-wise combine":

    >>> df1 = pd.DataFrame({'A': [5, 0], 'B': [2, 4]})
    >>> df2 = pd.DataFrame({'A': [1, 1], 'B': [3, 3]})
    >>> df1.combine(df2, np.minimum)
       A  B
    0  1  2
    1  0  3
    

提交回复
热议问题