Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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
like image 201
user2464433 Avatar asked Jun 07 '13 17:06

user2464433


People also ask

How do you combine two Series in Python?

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) .

How do you stack 2 Series pandas?

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.

Can we create DataFrame from multiple 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.


3 Answers

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
like image 101
BrenBarn Avatar answered Oct 12 '22 18:10

BrenBarn


I find this the simplest:

import numpy as np

smax = np.minimum(s1, s2)

Link to docs (numpy.minimum)

like image 20
Ankur Kanoria Avatar answered Oct 12 '22 17:10

Ankur Kanoria


Another similar way:

In [11]: pd.DataFrame([s1, s2]).min()
Out[11]:
1    1
2    1
3    1
4    1
dtype: float64
like image 27
Andy Hayden Avatar answered Oct 12 '22 17:10

Andy Hayden