0

How findout the one which comes first among max and min in a rolling window of size n on dataframe and store the quantified values(lets say 1 if max comes first and 0 if min comes first) ?

cs95
  • 379,657
  • 97
  • 704
  • 746
  • 2
    Welcome to StackOverflow. Please take the time to read this post on [how to provide a great pandas example](http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) as well as how to provide a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve) and revise your question accordingly. These tips on [how to ask a good question](http://stackoverflow.com/help/how-to-ask) may also be useful. – jezrael Jun 13 '18 at 12:14

1 Answers1

2

You could try something like this:

np.random.seed(124)
df = pd.DataFrame({'data':np.random.randint(0,10,25)})
df['First'] = df['data'].rolling(5, min_periods=2).apply(lambda x: x.idxmin()<x.idxmax(), raw=False)
df['First'] = df['First'].map({1:'Min',0:'Max'})
df

Output:

    data First
0      1   NaN
1      7   Min
2      2   Min
3      9   Min
4      0   Max
5      4   Max
6      4   Max
7      5   Max
8      5   Min
9      6   Min
10     9   Min
11     6   Min
12     0   Max
13     8   Max
14     9   Max
15     9   Min
16     0   Min
17     2   Max
18     2   Max
19     1   Max
20     1   Min
21     9   Min
22     1   Min
23     9   Min
24     4   Min
Scott Boston
  • 147,308
  • 15
  • 139
  • 187
  • why the exact same code is giving error "' apply() got an unexpected keyword argument 'raw' and without raw its giving " 'numpy.ndarray' object has no attribute 'idxmin'' – Sourav Dalai Jun 14 '18 at 06:02