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

前端 未结 8 1490
南方客
南方客 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:45

    The most straightforward way I can see is to make them into a DataFrame and then take the row-wise min:

    >>> print pandas.concat([s1, s2], axis=1).min(axis=1)
    1    1
    2    1
    3    1
    4    1
    dtype: float64
    
    0 讨论(0)
  • 2020-12-29 18:47

    This method will do the job:

    import pandas as pd
    
    
    def elementwise_min(x, y):
        x[x > y] = y
        return x
    
    
    a = pd.Series([1, 2, 3])
    b = pd.Series([0, 2, 4])
    elementwise_min(a, b)
    
    0 讨论(0)
提交回复
热议问题