**New course!**Every coder should learn

**Generative AI!**

0

# Data Science - Missing Numbers Project

I can't understand why it doesn't accept my code. It request to fill NaN with the mean. I do that and it doesn't work. I don't get it. import numpy as np import pandas as pd lst = [float(x) if x != 'nan' else np.NaN for x in input().split()] lst_df = pd.DataFrame(lst) print(lst_df.fillna(lst_df.mean()[0]).round(1)[0]) Can someone help me please? I also tried this and even though it outputs the requested numbers, it doesn't accept as correct. Maybe it is a bug on SoloLearn side. import numpy as np import pandas as pd lst = [float(x) if x != 'nan' else np.NaN for x in input().split()] lst_df = pd.DataFrame(lst) final = lst_df.fillna(lst_df.mean()).round(1) for i in final[0]: print(i)

20 Answers

+ 33

your output and the expected output are not the same, at the end of the expected output there is also: dtype: float64
use pd.Series instead of DataFrame
no need to print each element one by one
lst_df = pd.Series(lst)
final = lst_df.fillna(lst_df.mean()).round(1)
print(final)

+ 13

import numpy as np
import pandas as pd
lst = [float(x) if x != 'nan' else np.NaN for x in input().split()]
lst_df = pd.Series(lst)
abdullah = lst_df.fillna(lst_df.mean()).round(1)
print(abdullah)

+ 3

Thanks a lot dude. I ll try that. I am drinking beers now. But ll try that later.

+ 3

import numpy as np
import pandas as pd
lst = [int(x) if x != 'nan' else np.nan for x in input().split()]
df = pd.Series(lst)
df = df.fillna(df.mean()).round(1)
print(df)
hi, all i tried this code i cleared two test cases but the remaining are hidden please help to solve this

+ 3

Akash Poovan Use float(x) instead int(x) because we still consider about the decimal too

+ 3

This work nice:
import numpy as np
import pandas as pd
lst = [float(x) if x != 'nan' else np.nan for x in input().split()]
df = pd.Series(lst)
df = df.fillna(df.mean()).round(1)
print(df)

+ 2

Dude. Even drunk I tried that right now and it worked. Thanks a lot. May Odin receive you in Valhalla with Meat and Ale.

+ 2

what the.. you guys give answers using fillna, which is not include the course. I'm a beginner, as many others, how can we resolve this challenge while we are not taught about it?
I tried:
new_lst = [i if i != 'NaN' else mean for i in lst]
but ofcourse it failed. I cannot figure out what 'np.NaN' value is, even I tried:
new_lst = [i if i != np.NaN else mean for i in lst]
or many things like null, '', ' ', None,... still failed

+ 1

Bahha🐧 Thanks! Had the same problem

+ 1

everyone in this project used fillna attiribute. but so far , as beginners , we didnt learn this attribute. in this condition;
1) how can i find there is a attribute for this function? ( yes i know i can find it in forums, but it wont happen always.) is there any easy way to find out?
2)to solve this problem without using "fillna", i need to know what is the value of np.nan elements. for example
for x in lst :
if x != "nan": #it always true even if the value is np.nan
...
...
could you help tahnks
sory for my language :(

+ 1

Did someone know how
lst = [float(x) if x != 'nan' else np.NaN for x in input().split()]
Function works? I still don't understand

+ 1

Sylvanast
Code just look the input and if it is not "Nan" returns float x else return np.nan

+ 1

To answer Sylvanast question
lst = [float(x) if x != 'nan' else np.NaN for x in input().split()]
this function works by splitting up a string of values and turning the values into a floating point (values) if x does not equal 'nan' else if 'nan' replace with np.NaN (Not a Number) for every value in the input

+ 1

Use df.fillna(df.mean(), inplace=True) if you don't use df = df.fillna(.....)

+ 1

import numpy as np
import pandas as pd
lst = [float(x) if x != 'nan' else np.NaN for x in input().split()]
nplst=np.array(lst) #convert input to numpy array
lst=list(nplst[~np.isnan(nplst)]) #removing nan values to calculate mean
x=round(np.array(lst).mean(),1) #the mean calculation
y=np.nan_to_num(nplst,nan=x) #replacing the nan with calculated mean
nplst=pd.Series(y)
print(nplst)

+ 1

import numpy as np
import pandas as pd
lst = [float(x) if x != 'nan' else np.NaN for x in input().split()]
a=np.array(lst)
mean=pd.Series(lst).mean().round(1)
mask=(np.isnan(a)) #checks if element is NaN and returns a masked array
a[mask]=mean #replaces mask with mean
print(pd.Series(a))

+ 1

I changed the given code a little bit to solve the case in beginner level :
first I changed lst as follows:
lst = [float(x) if x != 'nan' else 'nan' for x in input().split()]
then I used two for loops to find the average and replace 'nan' with it
average= 0
count= 0
for l in lst:
if k !='nan':
average+=k
count+=1
else:
continue
average = round(average/count,1)
for i in range(len(lst)):
if lst[i] == 'nan':
lst[i] = average
print (pd.Series(lst))

0

try this dummy code:
import numpy as np
import pandas as pd
lst = [float(x) if x != 'nan' else np.nan for x in input().split()]
din = pd.Series(lst)
din = din.fillna(df.mean()).round(1)
print(din)

0

import numpy as np
import pandas as pd
lst = [float(x) if x != 'nan' else np.NaN for x in input().split()]
ds = pd.Series(lst)
print(ds.fillna(ds.mean()).round(1))

0

import numpy as np
import pandas as pd
lst = [float(x) if x != 'nan' else np.NaN for x in input().split()]
ds = pd.Series(lst)
print(ds.fillna(ds.mean()).round(1))