Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access individual elements within a rolling window on a dataframe

I have a dataframe with the quarterly U.S. GDP as column values. I would like to look at the values, 3 at a time, and find the index where the GDP fell for the next two consecutive quarters. This means I need to compare individual elements within df['GDP'] with each other, in groups of 3.

Here's an example dataframe.

df = pd.DataFrame(data=np.random.randint(0,10,10), columns=['GDP'])
df

    GDP
0   4
1   4
2   4
3   1
4   4
5   4
6   8
7   2
8   3
9   9

I'm using df.rolling().apply(find_recession), but I don't know how I can access individual elements of the rolling window within my find_recession() function.

gdp['Recession_rolling'] = gdp['GDP'].rolling(window=3).apply(find_recession_start)

How can I access individual elements within the rolling window, so I can make a comparison such as gdp_val_2 < gdp_val_1 < gdp_val?

The .rolling().apply() will go through the entire dataframe, 3 values at a time, so let's take a look at one particular window, which starts at index location 6:

   GDP
6  8   # <- gdp_val
7  2   # <- gdp_val_1
8  3   # <- gdp_val_2

How can I access gdp_val, gdp_val_1, and gdp_val_2 within the current window?

like image 393
Codedorf Avatar asked Nov 09 '22 05:11

Codedorf


1 Answers

Using a lambda expression within .apply() will pass an array into the custom function (find_recession_start), and so I can just access the elements as I would any list/array e.g. arr[0], arr[1], arr[2]

df = pd.DataFrame(data=np.random.randint(0,10,10), columns=['GDP'])

def my_func(arr):
    if((arr[2] < arr[1]) & (arr[1] < arr[0])):
        return 1
    else:
        return 0

df['Result'] = df.rolling(window=3).apply(lambda x: my_func(x))
df

    GDP Result
0   8   NaN
1   0   NaN
2   8   0.0
3   1   0.0
4   9   0.0
5   7   0.0
6   9   0.0
7   8   0.0
8   3   1.0
9   9   0.0
like image 158
Codedorf Avatar answered Nov 14 '22 21:11

Codedorf