Hristo Stoychev
Hristo Stoychev

Reputation: 535

Python Pandas apply value

I have a df with several columns: 'hour', 'day', 'week', 'month', 'year' and 'value'. I've grouped by 'week' and 'hour' with 'value' looking for mean():

df_group = df.groupby(['week','hour']).value.mean().reset_index()

Now I want to apply that mean value as a separate column for each hour in each week. Any ideas? THanks in advance!

Upvotes: 1

Views: 37

Answers (1)

jezrael
jezrael

Reputation: 862681

I think you need transform if need add new column to original data:

df['new'] = df.groupby(['week','hour']).value.transform('mean')

Upvotes: 1

Related Questions