I am working with a series in python, What I want to achieve is to get the highest value out of every n values in the series.
For example:
if n is 3
Series: 2, 1, 3, 5, 3, 6, 1, 6, 9
Expected Series: 3, 6, 9
I have tried nlargest function in pandas but it returns largest values in descending order, But I need the values in order of the original series.
CodePudding user response:
There are various options. If the series is guaranteed to have a length of a multiple of n, you could drop down to numpy and do a .reshape
followed by .max
along an axis.
Otherwise, if the index is the default (0, 1, 2, ...), you can use groupby
:
import pandas as pd
n = 3
ser = pd.Series([2, 1, 3, 5, 3, 6, 1, 6, 9])
out = ser.groupby(ser.index // n).max()
out:
0 3
1 6
2 9
dtype: int64