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
Combine Two Series Using pandas.merge() can be used for all database join operations between DataFrame or named series objects. You have to pass an extra parameter “name” to the series in this case. For instance, pd. merge(S1, S2, right_index=True, left_index=True) .
pandas's library allows two series to be stacked as vertical and horizontal using a built-in command called concat() . This action is usually performed to create a dataframe from two series.
You can create a DataFrame from multiple Series objects by adding each series as a columns. By using concat() method you can merge multiple series together into DataFrame. This takes several params, for our scenario we use list that takes series to combine and axis=1 to specify merge series as columns instead of rows.
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
I find this the simplest:
import numpy as np
smax = np.minimum(s1, s2)
Link to docs (numpy.minimum)
Another similar way:
In [11]: pd.DataFrame([s1, s2]).min()
Out[11]:
1 1
2 1
3 1
4 1
dtype: float64
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With