简体   繁体   中英

Filling of NaN values with the average of Quantity corresponding to a particular year

Year    Week_Number DC_Zip  Asin_code
1   2016    1   84105         NaN
2   2016    1   85034         NaN
3   2016    1   93711         NaN
4   2016    1   98433         NaN
5   2016    2   12206        21.0
6   2016    2   29306        10.0
7   2016    2   33426        11.0
8   2016    2   37206        1.0
9   2017    1   12206        266.0
10  2017    1   29306        81.0
11  2017    1   33426        NaN
12  2017    1   37206        NaN
13  2017    1   45216        99.0
14  2017    1   60160        100.0
15  2017    1   76110        76.0
16  2018    1   12206        562.0
17  2018    1   29306        184.0
18  2018    1   33426        NaN
19  2018    1   37206        NaN
20  2018    1   45216        187.0
21  2018    1   60160        192.0
22  2018    1   76110        202.0
23  2019    1   12206        511.0
24  2019    1   29306        NaN
25  2019    1   33426        224.0
26  2019    1   37206        78.0
27  2019    1   45216        160.0
28  2019    1   60160        NaN
29  2019    1   76110        221.0
30  2020    6   93711        NaN
31  2020    6   98433        NaN
32  2020    7   12206        74.0
33  2020    7   29306        22.0
34  2020    7   33426        32.0
35  2020    7   37206        10.0
36  2020    7   45216       34.0

I want to fill the NaN values with the Average of Asin_code for that particular year.I am able to fill the values for 2016 with this code

df["Asin_code"]=df.Asin_code.fillna(df.Asin_code.loc[(df.Year==2016)].mean(),axis=0)

But unable to do with the whole dataframe..

Use groupby().transform() and fillna :

df['Asin_code'] = df['Asin_code'].fillna(df.groupby('Year').Asin_code.transform('mean'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM