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.
Who among the following sits diagonally opposite to X?
Seven persons, J, K, L, M, N, O and P sit in a linear row and face in north direction. Only two persons sit between P and J. M sits adjacent to L. Only ...
Five persons M, N, O, P and Q are sitting in a row facing north. M is sitting third to the right of N. P sits second to the left of M. Q sits at one of ...
Who among the following sits at one of the extreme ends?
Who among the following person likes Red?
Who among the following likes Cauliflower?
How many persons sit to the right of X?
What is the position of A with respect to W?
Who among the following person sits second to the left of V?
Seven friends A, B, C, D, E, F and G are sitting around a circular table facing towards the centre(not necessarily in the same order). A is third to the...