2

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)

19th Dec 2020, 5:00 PM
Robson Marini
Robson Marini - avatar
20 Answers
+ 28
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)
19th Dec 2020, 6:30 PM
Bahha🐧
Bahha🐧 - avatar
+ 9
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)
5th Mar 2021, 5:30 PM
Abdullah Abdelhakeem
Abdullah Abdelhakeem - avatar
+ 4
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)
11th Apr 2021, 7:38 PM
Davor Budimir
Davor Budimir - avatar
+ 3
Thanks a lot dude. I ll try that. I am drinking beers now. But ll try that later.
19th Dec 2020, 8:15 PM
Robson Marini
Robson Marini - avatar
+ 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
15th Mar 2021, 11:10 AM
Akash Poovan
Akash Poovan - avatar
+ 3
Akash Poovan Use float(x) instead int(x) because we still consider about the decimal too
15th Mar 2021, 11:12 AM
Sylvanast
Sylvanast - avatar
+ 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.
19th Dec 2020, 9:52 PM
Robson Marini
Robson Marini - avatar
+ 1
Bahha🐧 Thanks! Had the same problem
25th Feb 2021, 10:59 AM
Coalemus
Coalemus - avatar
+ 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 :(
6th Apr 2021, 9:35 PM
Osman Bahadir Yilmaz
Osman Bahadir Yilmaz - avatar
+ 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
9th Apr 2021, 8:32 AM
Sylvanast
Sylvanast - avatar
+ 1
Sylvanast Code just look the input and if it is not "Nan" returns float x else return np.nan
11th Apr 2021, 8:46 PM
Osman Bahadir Yilmaz
Osman Bahadir Yilmaz - avatar
+ 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
16th Apr 2021, 2:42 PM
Taylor Packard
Taylor Packard - avatar
+ 1
Use df.fillna(df.mean(), inplace=True) if you don't use df = df.fillna(.....)
21st Jul 2021, 5:24 AM
POON Wai Sum
POON Wai Sum - avatar
+ 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)
31st Jul 2021, 8:05 AM
Abdulrhman Abduallah Elshiekh Idres
Abdulrhman Abduallah Elshiekh Idres - avatar
+ 1
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
5th Sep 2021, 10:39 AM
Viet Anh Nguyen
Viet Anh Nguyen - avatar
+ 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))
4th Jun 2022, 5:35 AM
Aaron Thomas
Aaron Thomas - avatar
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)
27th Apr 2021, 12:40 PM
Mardin D. Maloloy-on
Mardin D. Maloloy-on - avatar
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))
16th Jun 2021, 2:12 PM
Pramod Ugale
Pramod Ugale - avatar
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))
20th Apr 2022, 6:35 AM
Minnie Daryl M. Villanueva
Minnie Daryl M. Villanueva - avatar
0
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))
26th Nov 2022, 9:45 AM
Ali Dolat
Ali Dolat - avatar