The Python Oracle

Pandas groupby ewm

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Flying Over Ancient Lands

--

Chapters
00:00 Pandas Groupby Ewm
01:59 Answer 1 Score 0
02:13 Accepted Answer Score 8
02:25 Answer 3 Score 1
02:36 Thank you

--

Full question
https://stackoverflow.com/questions/5800...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #pandas #timeseries #pandasgroupby #rollingcomputation

#avk47



ACCEPTED ANSWER

Score 8


Let's fix the problem, using transform:

t['ewm'] = ts.groupby(['C1'])['X1'].transform(lambda x: x.ewm(halflife=10).mean()).values()



ANSWER 2

Score 1


The accepted answer is very slow for large datasets.

What I have done is:

ts['ewm'] = ts.groupby(['C1']).ewm(halflife=10).mean().values

and it seems to work just fine




ANSWER 3

Score 0


Can you try this? Do NOT set ts = df.set_index('T'). Then you can do as below

ts['ewm']=ts.groupby(['C1'], sort=False).apply(lambda x: x['X1'].ewm(halflife=10).mean()).reset_index(drop=True)