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.
Dr Manik shah has taken oath as the new Chief Minister of which of the following states?
Which organization received the ‘Global Water Tech Award 2024’ from the Global Energy & Environment Foundation for its environmental conservation ef...
A four year mentorship programme ‘Project Arohan’ to provide guidance to students which state government has launched it recently?
The Home ministry has constituted a three-member team to inquire into serious security breach during Prime Minister Narendra Modi's visit to Punjab. It ...
Durgesh Aranya Zoological Park in Kangra, Himachal Pradesh, aiming to become India's first IGBC-certified eco-friendly zoo, aims to cover how many hecta...
Which organization manages the Maitri Research and Cultural Partnerships Grants?
Which book provides a comprehensive analysis of the individuals who served as the chief of the Reserve Bank of India?
Consider the following statements :
I . Recently India and Sweden signed a MoU on cooperation in the field of Water Resources Development and M...
Which of the following state government has set a target to purchase 11 million tonnes of paddy in the current Kharif marketing season at the minimum su...
How much has been disbursed by the government till July ,2023 under the Aatmanirbhar Bharat Rojgar Yojana (ABRY) since the launch of the scheme in Octob...