# Python Group By

I am new to Python and am trying a simple calculation. I have a data frame with 1000 observations for different years and want to calculate the mean of a variable by year. I've used the "groupby" statement, but the resulting means only occur once per year. How can I have repeating mean values for all 1000 observations in the original dataset? Is there a way to do this without merging grouped results to the original data frame?

Edit: using data from this link as an example, if I have the following: Is there a way to calculate this mean in the original data and have it for every row? • There are many examples of how to do groupby operations in Python. If you are not already, I would suggest using the Pandas library. Here is a walkthrough/tutorial: marsja.se/python-pandas-groupby-tutorial-examples. Otherwise, It'd be helpful if you showed the code you have so far. – n1k31t4 Aug 11 '19 at 23:43
• I just added an example using the data from this link. – teal_sky Aug 12 '19 at 0:04

Best method will be to calculate mean salary against as you have done in 1st dataframe example and then use joins,mapping,apply etc. to get desired dataframe.

1. Join You can use right outer join to merge both dataframes in your examples, considering 1st dataframe as left and 2nd datafarame as right. See the documentation.

2. By using apply:

dat['salary'] = dat['rank'].apply(lambda x :93876.437500 if x =='Assoc Prof' else ... and so on )

3. Map Its also similar to apply

dat['salary'] = dat['rank'].map('Assoc Prof':93876.437500 , '': , '' : ...and so on)

I think you are looking for a partitionBy method. This can be done in pandas using groupBy and the transform method as seen in this link: https://stackoverflow.com/questions/35905335/aggregation-over-partition-pandas-dataframe