3

This question is similar to my previous one: Shifting elements of column based on index given condition on another column

I have a dataframe (df) with 2 columns and 1 index.

Index is datetime index and is in format of 2001-01-30 .... etc and the index is ordered by DATE and there are thousands of identical dates (and is monthly dates). Column A is company name (which corresponds to the date), Column B are share prices for the company names in column A for the date in the Index.

Now there are multiple companies in Column A for each date, and companies do vary over time (so the data is not predictable fully).

I want to create a column C which has the 3 day rolling exponential weighting average of the price for a particular company using the current and 2 dates before for a particular company in column A.

I have tried a few methods but have failed. Thanks.

Community
  • 1
  • 1

1 Answers1

2

Try:

df.groupby('ColumnA', as_index=False).apply(lambda g: g.ColumnB.ewm(3).mean())
Zeugma
  • 31,231
  • 9
  • 69
  • 81