Suppose I have 2 Series in pandas:
from datetime import datetime, timedelta
import pandas as pd
d = datetime.now()
index = [d + timedelta(seconds = i) for i in range(5)]
a = pd.Series([1,4,5,7,8], index = index)
b = pd.Series([2,3,6,7,8], index = index)
What is the best way to get min/max values for corresponding index elements. Like:
min_func(a, b): [1,3,5,7,8] (for given index)
max_func(a, b): [2,4,6,7,8]
The only functions I could find in the documentation are min/max functions that return min/max within the series, while .apply function doesn't take the index argument. Is there a better way to implement that without manual series iteration or some arithmetic magic (like min_func: a * (a < b) + b * (b <= a), max_func: a * (a > b) + b * (b >= a) )
Thanks
Pandas DataFrame min() Method The min() method returns a Series with the minimum value of each column. By specifying the column axis ( axis='columns' ), the max() method searches column-wise and returns the minimum value for each row.
In the pandas series constructor, there is a method called gt() which is used to apply the Greater Than condition between elements of two pandas series objects. The result of the gt() method is based on the comparison between elements of two series objects.
To get the position of minimum value of a pandas series object we can use a function called argmin(). The argmin() is the method of the pandas series constructor, which is used to get the row position of the smallest value from the series. The output of the argmin() method is an integer value.
Get the maximum values of every column in Python To find the maximum value of each column, call the max() method on the Dataframe object without taking any argument. In the output, We can see that it returned a series of maximum values where the index is the column name and values are the maxima from each column.
Combine the series into a frame which automatically aligns by the index
In [51]: index
Out[51]:
[datetime.datetime(2013, 8, 26, 18, 33, 48, 990974),
datetime.datetime(2013, 8, 26, 18, 33, 49, 990974),
datetime.datetime(2013, 8, 26, 18, 33, 50, 990974),
datetime.datetime(2013, 8, 26, 18, 33, 51, 990974),
datetime.datetime(2013, 8, 26, 18, 33, 52, 990974)]
In [52]: a = pd.Series([1,4,5,7,8], index = index)
In [53]: b = pd.Series([2,3,6,7,8], index = index)
In [54]: a
Out[54]:
2013-08-26 18:33:48.990974 1
2013-08-26 18:33:49.990974 4
2013-08-26 18:33:50.990974 5
2013-08-26 18:33:51.990974 7
2013-08-26 18:33:52.990974 8
dtype: int64
In [55]: b
Out[55]:
2013-08-26 18:33:48.990974 2
2013-08-26 18:33:49.990974 3
2013-08-26 18:33:50.990974 6
2013-08-26 18:33:51.990974 7
2013-08-26 18:33:52.990974 8
dtype: int64
In [56]: df = DataFrame({ 'a' : a, 'b' : b })
In [57]: df
Out[57]:
a b
2013-08-26 18:33:48.990974 1 2
2013-08-26 18:33:49.990974 4 3
2013-08-26 18:33:50.990974 5 6
2013-08-26 18:33:51.990974 7 7
2013-08-26 18:33:52.990974 8 8
Min/Max
In [9]: df.max(1)
Out[9]:
2013-08-26 18:33:48.990974 2
2013-08-26 18:33:49.990974 4
2013-08-26 18:33:50.990974 6
2013-08-26 18:33:51.990974 7
2013-08-26 18:33:52.990974 8
Freq: S, dtype: int64
In [10]: df.min(1)
Out[10]:
2013-08-26 18:33:48.990974 1
2013-08-26 18:33:49.990974 3
2013-08-26 18:33:50.990974 5
2013-08-26 18:33:51.990974 7
2013-08-26 18:33:52.990974 8
Freq: S, dtype: int64
Index of min/max
In [11]: df.idxmax(1)
Out[11]:
2013-08-26 18:33:48.990974 b
2013-08-26 18:33:49.990974 a
2013-08-26 18:33:50.990974 b
2013-08-26 18:33:51.990974 a
2013-08-26 18:33:52.990974 a
Freq: S, dtype: object
In [12]: df.idxmin(1)
Out[12]:
2013-08-26 18:33:48.990974 a
2013-08-26 18:33:49.990974 b
2013-08-26 18:33:50.990974 a
2013-08-26 18:33:51.990974 a
2013-08-26 18:33:52.990974 a
Freq: S, dtype: object
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