-2

I have a data frame given below

[![enter image description here][1]][1]
example for year 2016 'div' should only have sum of 'avg_coef' across only 2016 
and same for 2017 'div' should only have sum of 'avg_coef' across only 2017 and not any other value

I have almost achieved this by applying lambda but when applying it is only working for the last year in the loop and assigning other value in div as 0 as it move ahead in the loop, below is the code and output it is generating

[![enter image description here][1]][1]
[![enter image description here][1]][1]

some how it is only working for last year which is 2021, please also suggest you have a more optimized way of doing it, Thanks you all

  • 1
    Please, see [Why should I not upload images of code/data/errors when asking a question?](https://meta.stackoverflow.com/q/285551/14627505) and provide a [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). Here is how to make a good one [in pandas](https://stackoverflow.com/q/20109391/14627505). – Vladimir Fokow Aug 29 '22 at 06:11

1 Answers1

0

You can use .transform()

df['div'] = df.groupby('Year')['avg_coef'].transform(sum)
Vladimir Fokow
  • 3,728
  • 2
  • 5
  • 27