Pandas DF related problem - MISSING NUMBERS | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Pandas DF related problem - MISSING NUMBERS

''' In DS with Py, one of the end of module tasks, 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. I have solved this challenge by not doing exactly the same as instructed and that's why even after solving the challenge, I am stuck here trying to find a way to do as said. ''' 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(round(df.mean(),1)) print (df) Whenever I change that to df = pd.DataFrame(lst) Column name is automatically given as 0, which is expected but When I try to return that column like this Print (df.iloc[:,0] It prints everything fine but has one extra Name= 0 (for that column name) in the result and due to that, test doesn't pass. Can you suggest to get the expected output using pd.DataFrame??? Thanks in Advance!!!

11th Apr 2021, 4:23 AM
CHANDAN ROY
CHANDAN ROY - avatar
5 Answers
+ 3
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(round(df.mean(),1)) print (df)
19th Aug 2022, 10:12 AM
Muzaffar Bekturdiyev
0
Tony F 🐙 thanks for your response. I had solved this challenge before writing this query. I just wanted to know if there is a solution to the questions I have raised?
21st May 2021, 7:17 AM
CHANDAN ROY
CHANDAN ROY - avatar
0
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(round(df.mean(),1)) print (df) this code will more efficient ... and thanks a lot for giving hint...
3rd Sep 2022, 2:20 PM
Shaheen R Tirlapur
0
To create a DataFrame with a single column and avoid the extra column name, you can modify your code as follows: import numpy as np import pandas as pd lst = [float(x) if x != 'nan' else np.NaN for x in input().split()] df = pd.DataFrame({0: lst}) # Create a DataFrame with a dictionary specifying column name as 0 df = df.fillna(round(df.mean(), 1)) print(df.iloc[:, 0]) By passing a dictionary {0: lst} to the pd.DataFrame() constructor, you explicitly assign the column name as 0 instead of letting pandas assign the default name. This will result in a DataFrame with a single column named 0, matching the expected output. https://www.mylifetouch.us/
10th May 2023, 9:53 AM
Sarah Ford
- 1
🅰🅹 (Challenge Accepted) Is there a way to remove that thing??
12th Apr 2021, 4:29 AM
CHANDAN ROY
CHANDAN ROY - avatar