简体   繁体   中英

Fillna Pandas NaN with mean and median

I'm starting with python and data science, I have a .csv file with more than 5000 lines. I want to replace Exerience NaN values with mean for Data scientist and median for data engineer. How can I group this and use fillna. 在此处输入图片说明

Each time I try to use fillna with mean() I have this error :

TypeError: can only concatenate str (not "int") to str NaN

Assuming that you have this table loaded in Pandas in a variable named df .
Also assuming that when you say mean and median you mean of the Experience column.

df.loc[
    (df["Metier"] == "Data scientist") & (df["Experience"].isnull()), "Experience"
] = df["Experience"].mean()

df.loc[
    (df["Metier"] == "Data engineer") & (df["Experience"].isnull()), "Experience"
] = df["Experience"].median()

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