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.
- What is the currency of Uruguay?
Which agreement adopted during the Philippines President’s state visit to India will govern joint army-level interactions?
How many megawatts will each of the four units in the Tato-II project generate?
What is the theme of SEFCO-2025 organized by ?
Consider the following statements:
I. Jivhala scheme is related to karnataka state.
II. It is primarily for inmates who are undergoing a p...
Where was the 40th National Youth Basketball Championships held?
Which oil - producing country recently announced its decision to leave the Organization of the Petroleum Exporting Countries ( OPEC ) ?
How frequently will Bengaluru’s digital population clock update Karnataka’s population count?
___________ is mutual funds writing off the principal amount and the interest in case of a default?
Under DAJA, how many tribal citizens were reached in the first 9 days of the campaign?