0
Did anyone do DS with Python the Missing Numbers assignment
Did someone did 3rd challenge from DS with python course? Mine always causes error. Here’s my code: import numpy as np import pandas lst = [float(x) if x != 'nan' else np.NaN for x in input().split()] from numpy import nan lst.fillna(lst.mean(), inplace=True) print(lst.isnull().sum()) DM please to explain!
7 ответов
+ 2
lst is a list. I think it should be a pandas dataframe.
+ 1
...series....and suddenly it is so easy. Thank you, Lisa!
import numpy as np
import pandas as pd
lst = [float(x) if x != 'nan' else np.NaN for x in input().split()]
ser = pd.Series(lst)
mean= round(ser.mean(),1)
ser = ser.where(ser.notna(),mean)
print(ser)
0
I have the same problem. The codes calculates the values correct, but when I print it, the result is correct, but the output ist missing the "dtype: float64" part. Adding a print(df.dtypes) doesn't work either, because the output states dtype object....
0
Trochoide
What type does your result have?
0
Lisa
I cannot copy paste the result, nor can I Post a Screenshot (or can I?), so I am typing the output
0 3.0
....
....
....
6 3.8
floating64
dtype: object
code:
df=pd.DataFrame(lst, columns=[""])
print(df)
print(df.dtypes)
0
Trochoide
You should be able to copy your code. Then you could save it in playground and link it.
I looked it up how I solved it: I converted lst to a pandas series, replaced the nan and only printed the series in the end (it was float64)
0
import numpy as np
import pandas as pd
lst = [float(x) if x != 'nan' else np.NaN for x in input().split()]
ser=pd.Series(lst)
mean_val = round(ser.mean(),1)
ser1=ser.fillna(value=mean_val)
print(ser1)