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

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

    You can concat the dataframes and take the minimum, specifying level=0:

    >>> s1 = pd.Series(data=[1,1,1], index=[1,2,3])
    >>> s2 = pd.Series(data=[1,2,2,1], index=[1,2,3,4])
    >>> pd.concat([s1, s2]).min(level=0)
    1    1
    2    1
    3    1
    4    1
    dtype: int64
    

    This approach also works on dataframes.

提交回复
热议问题