How to assign the average of a grouped dataframe to a column of another dataframe in python and pandas

I have an original dataframe like the example below

data_original = {'website': ['a', 'b'], 'unit': ['finance', 'business']}
df_original = pd.DataFrame(data_original)

And it gave rise to the dataframe:

enter image description here

I have another dataframe, df with the following:

data = {'date': ['1/1/2021', '1/2/2021', '1/3/2021', '1/4/2021', '1/5/2021', '1/1/2021', '1/2/2021', '1/3/2021', '1/4/2021',
             '1/5/2021'], 'website': ['a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'b'], 'amount_views': [23, 17, 10, 25, 2, 12, 7, 5, 17, 2]}

df = pd.DataFrame(data)

This gave rise to the following:

enter image description here

I want to get the average views of each website in the second dataframe and add it as a column in the original dataframe.

I already did this:

df_new = df.groupby(['website']).mean()
df_original['average_views'] = df_new

And I got this instead

enter image description here

How do I just get the average and add it to the original dataframe?

Got the answer from Stack overflow as:

pd.merge(df_original,
         df.groupby('website')['amount_views'].mean().to_frame('average_views'),
         left_on='website',right_index=True,how='left')
1 Like