Moving Average Pandas

Compute moving average in pandas with a twist

Just use a for loop

for i in range(len(df)):
if df["Flag"].iloc[i]:
df["Value"].iloc[i] = df.loc[:,"Value"].iloc[i-4:i].mean()

how to calculate a moving average in a dataframe?

The error you're getting is telling you that it's not an issue with your .rolling() method but there is no .float() attribute of series in pandas, so, you should use pandas.DataFrame.astype() function to manipulate column dtypes.

test["CurrentCreditLines"].rolling(min_periods=1, center=True, window=12).mean().round()

test["CurrentCreditLines"].astype(float)

Is there a Pandas function to create a 7-day moving average from a time-series?

You can use .rolling() to calculate moving averages, like this:

df.rolling(window=7).mean() 

Rolling() also has an argument center=True to have the window calculated centred in the middle. Then it looks 3 days ahead and 3 days back when your window is 7.

The default value is center=False. It just takes current day value + 6 day values back when the window is 7.

Pandas - moving average for date with many measurements per date

It's not clear why you have multiple rows for each date. I see 2 possible interpretations:

  • Either these are values you have to aggregate at the star and then produce a single rolling average:
df.groupBy('Date').sum().rolling(5).mean().reset_index()
  • Or these are different products - then you still have to group by but instead of aggregating you can compute the rolling average for each product:
df.groupby('Product')['Date'].rolling(5).mean().reset_index()

Moving average 2 day span using Pandas

I think you need:

df['new'] = df['count'].rolling(5).mean()

count = df["new"].dropna().to_list()

print(count)

Output:

[23.0, 14.0]

Pandas pct_change with moving average

here is one way to do it, using rolling and shift

df['avg']=df.rolling(2).mean()
df['poc'] = (df['data'] - df['avg'].shift(+1))/ df['avg'].shift(+1)
df.drop(columns='avg')


data poc
0 1 NaN
1 2 NaN
2 3 1.0
3 7 1.8

How to plot moving average by groupby in python?

To get a moving average for time series data, the period of interest is specified differently: for 30 days, use '30D'. And since it is column-wise, we use loc to specify the column. Since it is already a single issue, groupby is not necessary. To create the graph, I used pandas visualization, which is the simplest way to do it.

df_A = new_df_A.copy()
df_A['Date'] = pd.to_datetime(df_A['Date'])
df_A.set_index('Date', inplace=True)

for cols in df_A.columns:
if cols not in ['Name', 'Date',]:
df_A['ma_'+cols] = df_A.loc[:,cols].rolling('30D').mean()

df_A.iloc[:,6:10].plot()

Sample Image

import seaborn as sns
sns.set(rc={'figure.figsize':(20,8)})

for cols in df_A.columns:
if cols not in ['Name', 'Date', 'Open', 'High', 'Close']:
sns.lineplot(x=df_A.index, y=df_A[cols])

#plt.show()

Sample Image

Moving average in pandas with condition

First replace not matched rows by NaN by Series.where and then per groups shift values and call rolling method:

f = lambda x: x.shift().rolling(3, min_periods=1).mean()
df['roll'] = (df.assign(x = df['x'].where(df['condition'].eq(1)))
.groupby('group')['x']
.transform(f))
print (df)
date group x condition roll
0 2020-01-01 A 1 1 NaN
1 2020-01-02 A 2 0 1.0
2 2020-01-03 A 2 1 1.0
3 2020-01-04 A 3 0 1.5
4 2020-01-01 B 2 1 NaN
5 2020-01-02 B 3 0 2.0
6 2020-01-03 B 4 1 2.0
7 2020-01-04 B 2 0 3.0

Details:

print (df.assign(x = df['x'].where(df['condition'].eq(1))))
date group x condition
0 2020-01-01 A 1.0 1
1 2020-01-02 A NaN 0
2 2020-01-03 A 2.0 1
3 2020-01-04 A NaN 0
4 2020-01-01 B 2.0 1
5 2020-01-02 B NaN 0
6 2020-01-03 B 4.0 1
7 2020-01-04 B NaN 0


Related Topics



Leave a reply



Submit