Numpy Conditional Max Range

I am trying to make my program version faster using as many Pandas and Numpy as possible. I'm new to Numpy, but grab most of it, but I'm having trouble conditionally formatting a column with a maximum range. This is the code I'm trying to use for this:

x=3
df1['Max']=numpy.where(df1.index>=x,max(df1.High[-x:],0))

Basically, I'm trying to conditionally put the maximum value for the last 3 records in a cell and repeat the column. Any help is appreciated.

+4
source share
2 answers

Use Scipy maximum_filter-

from scipy.ndimage.filters import maximum_filter1d

df['max'] = maximum_filter1d(df.High,size=3,origin=1,mode='nearest')

, maximum_filter , . max , . , origin. 1.

-

In [21]: df
Out[21]: 
   High  max
0    13   13
1    77   77
2    16   77
3    30   77
4    25   30
5    98   98
6    79   98
7    58   98
8    51   79
9    23   58

, Scipy slide max Pandas roll max . -

In [55]: df = pd.DataFrame(np.random.randint(0,99,(10000)),columns=['High'])

In [56]: %%timeit  # @Merlin rolling based solution :
    ...: df['max'] = df.High.rolling(window=3, min_periods=1).max()
    ...: 
1000 loops, best of 3: 1.35 ms per loop

In [57]: %%timeit  # Using Scipy max filter :
    ...: df['max1'] = maximum_filter1d(df.High,size=3,\
    ...: origin=1,mode='nearest')
    ...: 
1000 loops, best of 3: 487 µs per loop
+5

np.where

 numpy.where('test something,if true ,if false)

, .

dd= {'to': [100, 200, 300, 400, -500, 600, 700,800, 900, 1000]}

df = pd.DataFrame(dd)
df

         to
0   100
1   200
2   300
3   400
4  -500
5   600
6   700
7   800
8   900
9  1000

df['Max'] =  df.rolling(window=3, min_periods=1).max()


   to     Max
0   100   100.0
1   200   200.0
2   300   300.0
3   400   400.0
4  -500   400.0
5   600   600.0
6   700   700.0
7   800   800.0
8   900   900.0
9  1000  1000.0
+3

Source: https://habr.com/ru/post/1648824/


All Articles