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)
12/19/2020 5:00:45 PM
Robson Marini
19 Answers
New Answeryour 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)
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)
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
Akash Poovan Use float(x) instead int(x) because we still consider about the decimal too
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)
Dude. Even drunk I tried that right now and it worked. Thanks a lot. May Odin receive you in Valhalla with Meat and Ale.
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 :(
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
Sylvanast Code just look the input and if it is not "Nan" returns float x else return np.nan
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
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
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)
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))
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)
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))
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))