Question

In: Computer Science

Write a complete program in python to impute null values in a data frame with mean...

Write a complete program in python to impute null values in a data frame with mean and median . Please explain logic also because i am not understanding

Solutions

Expert Solution

To impute null values in Dataframe we use fillna(value ) method

This method will replace NA/NAN in the database with the Value provided in it

A simple program is

import numpy as np
import pandas as pd


# make a data having somve nan values in it
data = {"Column1": [np.nan, 7 , 610 , 757, np.nan,
49, 57, np.nan, 27, 503],
"Column2": [50, 71, 48, 232, 951 ,
97 , np.nan, 994, 29, np.nan ]}

# create a datavase of the above code
df = pd.DataFrame(data)
print(df)

# Now replace all NAN Values with mean of respective column values
df_mean= df.fillna(df.mean())
df_mean

#so we saw that all NAN values of column 1 are replaced by 287.142857 and Column 2 by 309.0

# Now replace all NAN Values with mean of respevtive column values
df_median= df.fillna(df.median())
df_median

#so we saw that all NAN values of column 1 are replaced by 57.0 and Column 2 by 84.0

Loigc is

df.mean() and df.median ()method computes means of whole data frame column wise And then when we use them in fillna() method it replaces corresponding column values with the mean or median of the that column only

See Code Screen Shot for Code after each parts

So we saw that to impute null values in data frame we can simply use fillna() method

Thank You

If u like the answer then Do Upvote the answer and have any douby comment it


Related Solutions

Write a complete Python program that asks the user for a line of text (not just...
Write a complete Python program that asks the user for a line of text (not just a single word), and then tells the user whether the string is a palindrome (same forward and backward) if you ignore case and non-alphabetic characters. The following methods and functions may be useful: str.upper(), str.isalpha() -> bool, reversed(str) -> sequence, str.find(str) -> int, str.replace(str, str), str.center(int). Unless otherwise specified, they all return a string.
Write a program IN PYTHON of the JUPYTER NOOTBOOK Write a Python program that gets a...
Write a program IN PYTHON of the JUPYTER NOOTBOOK Write a Python program that gets a numeric grade (on a scale of 0-100) from the user and convert it to a letter grade based on the following table. A: 90% - 100% B 80% - 89% C 70% - 79% D 60% - 69% F <60% The program should be written so that if the user entered either a non-numeric input or a numeric input out of the 0-100 range,...
write a python program that inputs 10 integer values from the keyboard and then displays their...
write a python program that inputs 10 integer values from the keyboard and then displays their sum. use for loop
Write a complete and syntactically correct Python program to solve the following problem: You are the...
Write a complete and syntactically correct Python program to solve the following problem: You are the payroll manager for SoftwarePirates Inc. You have been charged with writing a package that calculates the monthly paycheck for the salespeople. Salespeople at SoftwarePirates get paid a base salary of $2000 per month. Beyond the base salary, each salesperson earns commission on the following scale: Sales Commission Rate Bonus <$10000 0% 0 $10000 – $100,000 2% 0 $100,001 - $500,000 15% $1000 $500,001 -...
Complete the following in syntactically correct Python code. Write a program, using a for loop, that...
Complete the following in syntactically correct Python code. Write a program, using a for loop, that calculates the amount of money a person would earn over a period of time if his or her salary is 1 penny for the first day, 2 pennies for the second day, 4 pennies for the third day, and continues to double each day. 1.      The program should ask the user for the number of days the employee worked. 2.      Display a table showing the salary...
Please write in Python code Write a program that stores the following data in a tuple:...
Please write in Python code Write a program that stores the following data in a tuple: 54,76,32,14,29,12,64,97,50,86,43,12 The program needs to display a menu to the user, with the following 4 options: 1 – Display minimum 2 – Display maximum 3 – Display total 4 – Display average 5 – Quit Make your program loop back to this menu until the user chooses option 5. Write code for all 4 other menu choices
Write a python program for the following question. Complete the symptom similarity function, which measures the...
Write a python program for the following question. Complete the symptom similarity function, which measures the similarity between the symptoms of two patients. See below for an explanation of how the similarity is computed. def symptom_similarity(symptoms_A: Tuple[Set], symptoms_B: Tuple[Set]) -> int: '''Returns the similarity between symptoms_A and symptoms_B. symptoms_A and symptoms_B are tuples of a set of symptoms present and a set of symptoms absent. The similarity measure is computed by the following equations: present_present + absent_absent - present_absent -...
Exercise #3 python Write a program that could evaluate the relationship between two values. User is...
Exercise #3 python Write a program that could evaluate the relationship between two values. User is to key in the two values, and it will be stored in variable, num1 and num2. The program will do a comparison between the two values. If num1 is greater than num2, a message will be display on screen to indicate that num1 is greater than num2. The program will also display appropriate messages when num2 is greater than num1; and num1 is equal...
Write a python program. Grades are values between zero and 10 (both zero and 10 included),...
Write a python program. Grades are values between zero and 10 (both zero and 10 included), and are always rounded to the nearest half point. To translate grades to the American style, 8.5 to 10 become an “A,” 7.5 and 8 become a “B,” 6.5 and 7 become a “C,” 5.5 and 6 become a “D,” and other grades become an “F.” Implement this translation, whereby you ask the user for a grade, and then give the American translation. If...
Complete the program to read in values from a text file. The values will be the...
Complete the program to read in values from a text file. The values will be the scores on several assignments by the students in a class. Each row of values represents a specific student's scores. Each column represents the scores of all students on a specific assignment. So a specific value is a specific student's score on a specific assignment. The first two values in the text file will give the number of students (number of rows) and the number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT