Creating an element-wise minimum Series from two other Series in Python Pandas
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 [1]:
import pandas as pd
s1 = pd.Series(data=[1,1,1], index=[1,2,3])
s2 = pd.Series(data=[1,2,2,1], index=[1,2,3,4])
s1.add(s2)
Out[1]:
1 2
2 3
3 3
4 NaN
dtype: float64
But I cannot find an efficient way to do an element-wise minimum between two Series (along with aligning the indices and handling NaN values).
Nevermind. There is an escape hatch with the combine function so you can put in any element-wise function:
In [2]:
s1 = pd.Series(data=[1,1,1], index=[1,2,3])
s2 = pd.Series(data=[1,2,2,1], index=[1,2,3,4])
s1.combine(s2, min, 0)
Out[2]:
1 1
2 1
3 1
4 0
dtype: int64
Solution 1:
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
Solution 2:
I find this the simplest:
import numpy as np
smax = np.minimum(s1, s2)
Link to docs (numpy.minimum)
Solution 3:
Another similar way:
In [11]: pd.DataFrame([s1, s2]).min()
Out[11]:
1 1
2 1
3 1
4 1
dtype: float64