Missing values | Sololearn: Learn to code for FREE!


Missing values

In the course Data Science, I face a project. Although I coded it, and my output is the same as the example of the project, It was not accepted. I share the project and my code with you in order to receive assistance. Project definition: Imputing missing values. In the real world, you will often need to handle missing values. One way to impute (i.e., fill) the numerical column is to replace the null values with its mean. Task Given a list of numbers including some missing values, turn it into a pandas dataframe, impute the missing values with the mean, and finally return the dataframe. Sample Input 3 4 5 3 4 4 nan Sample Output 0 3.0 1 4.0 2 5.0 3 3.0 4 4.0 5 4.0 6 3.8 dtype: float64 My code import numpy as np import pandas as pd lst = [float(x) if x != 'nan' else np.NaN for x in input().split()] ar=np.array(lst) df=pd.DataFrame({'':ar}) ave=round(df[''].mean(),1) df=df.replace(np.nan,ave) print(df) print('dtype: ' + str(df[''].dtypes))

7/10/2021 7:19:10 PM


1 Answer

New Answer


The only thing you need Numpy for in this solution is np.NaN, the rest of your code should use pd. Take your list 'lst' and make a Series from it. Then fillna that df with the mean rounded to the 1st decimal point. Then output it.