userHG
userHG

Reputation: 589

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. enter image description here

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

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

Upvotes: 3

Views: 762

Answers (1)

eva-vw
eva-vw

Reputation: 670

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()

Upvotes: 4

Related Questions