Question
In Python, which method in the Pandas library would you
use to replace NaN values in a DataFrame with the median value of each column?Solution
The fillna() method in Pandas is used to replace NaN values in a DataFrame. By passing df.median() as an argument to fillna(), we can replace missing values with the median value of each column. This approach is especially useful when missing values are suspected to deviate from the mean due to outliers, making median imputation a more robust choice. The Pandas fillna() method is highly flexible and frequently used in data cleaning to handle missing data without discarding rows or losing valuable information in other columns. Option A (df.fillna(df.mean())) is incorrect as it fills NaNs with the mean rather than the median. Option B (df.replace(df.median())) is incorrect because replace() is not used directly for filling NaN values. Option D (df.dropna(inplace=True)) is incorrect as it removes rows with NaNs instead of filling them. Option E (df.interpolate(method="median")) is incorrect as interpolate() does not directly support median filling.
In a team of 25 players, the average age is 26 years. One player aged 24 left and five new players with an average age of 28 years joined. When the coac...
In an examination, Aryan scored 54% marks and passed by 98 marks, while Rohit scored 35% marks and failed by 35 marks. Determine the passing marks for t...
Average run scored by a batsman in first ‘x’ matches is 88. In next 10 matches he scored runs with an average of 100 so that his over...
Average run scored by a batsman in first ‘x’ matches is 55. In next 12 matches he scored runs with an average of 90 so that his overall average incr...
The average age of 23 children and their teacher's age are 21 years. If the teacher's age is excluded, the average reduces by 1. What is the teacher's age?
- The number of students in class 'A' is 108, and the ratio of girls to boys is 5:4. If the average number of boys in classes A and B is 114, and the average...
- The average age of (x + 6) students of a class is 16 years. If the 40 years old class teacher is also included then average age of the class gets increased...
Average weight of 5 students in a class increases by 4 kg when weight of teacher is also included. If sum of average weight of 5 students and weight of ...
Average of four numbers is 24. Sum of smallest and largest number is 53. If difference between other two numbers is 1, then find the square of second sm...
The average weight of a group of five persons, A, B, C, D, and E is 60 kg. If another person K joins the group then the average weight of the group is d...