I want to find mean of those rows in target variable corresponding to which there are null values in a particular column



Hello everyone,
I am doing data exploration on a data set in which I have 23 continuous variables and a target variable ‘total_cases’. In one of the columns ‘precipit’ of my data frame, I have 192 missing values. There are no missing values in my target variable. I want to find out the mean of total_cases corresponding to the rows which have NaN in ‘precip’ column. I am working on pythom.


@vikas_10 From what I have understood, you wish to calculate the mean of total_cases for those rows where the precipit has null values.
So, assuming that your dataframe’s name is ‘data’, try doing the following :

import pandas
precipit_is_null = pandas.isnull(data["precipit"])
null_precipit_dataset = data[precipit_is_null]
mean_total_cases = null_precipit_dataset["total_cases"].mean()

Hoping that this works, all the best!


Please try this :

import pandas as pd
col_null = pd.isnull(df[“precipit”])
col_null_dataset = df[col_null]
mean_find = col_null_dataset[“total_cases”].mean()

where df is your dataframe