How to calculate pandas DataFrame column values ​​depending on the results of the rolling function from another column


A very simple example to understand.

The goal is to compute the values ​​of the pandas DataFrame column based on the results of the rolling function from another column.

I have the following DataFrame:

import numpy as np
import pandas as pd

s = pd.Series([1,2,3,2,1,2,3,2,1])    
df = pd.DataFrame({'DATA':s, 'POINTS':0})

df

Beginning DataFrame

Note. I don’t even know how to format the results of the Jupyter Notebook in the Stackoverflow editing window, so I copy and paste the image, I apologize.

The DATA column shows the observed data; the POINTS column initialized to 0 is used to collect the output of the rolling function applied to the DATA column, as described below.

Set window = 4

nwin = 4

, "" max.

, , .

Algo flow

; POINT max DATA 1.

:

Dataframe end

python?

. ,
Gilberto

P.S. , Jupyter Notebook Stackoverflow? .

+4
2

IIUC @IanS ( !),

In [75]: np.array([df.DATA.rolling(4).max().shift(-i) == df.DATA for i in range(4)]).T.sum(axis=1)
Out[75]: array([0, 0, 3, 0, 0, 0, 3, 0, 0])

:

In [78]: df = pd.DataFrame({'DATA':s, 'POINTS':0})

In [79]: df.POINTS += np.array([df.DATA.rolling(4).max().shift(-i) == df.DATA for i in range(4)]).T.sum(axis=1)

In [80]: df
Out[80]: 
   DATA  POINTS
0     1       0
1     2       0
2     3       3
3     2       0
4     1       0
5     2       0
6     3       3
7     2       0
8     1       0
+2
import pandas as pd

s = pd.Series([1,2,3,2,1,2,3,2,1])    
df = pd.DataFrame({'DATA':s, 'POINTS':0})

df.POINTS=df.DATA.rolling(4).max().shift(-1)
df.POINTS=(df.POINTS*(df.POINTS==df.DATA)).fillna(0)
+1

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


All Articles