Search code examples
pythonindexingmissing-dataisnullfillna

fill missing value based on index


i try to percentace missing value like this

null = data.isnull().sum()*100/len(data)

and filter like this

null_column = null[(null>= 10.0) & (null <= 40.0)].index

the output type is index

how can i using fillna to replace median in every column based on index

my code before like this

null_column = null[(null>= 10.0) & (null <= 40.0)].index
data.fillna(percent_column2.median(), inplace=True)

the result always

index doesnt have median

but when i deleted index it works but the median that replaced is not median in every column. But, median that 2 values of percentage missing value not in original dataframe. How can i fill nan value based on index to replace in original data frame?


Solution

  • I guess something like this:

    data = pd.DataFrame([[0,1,np.nan],[np.nan,1,np.nan],[1,np.nan,2],[23,12,3],[1,3,1]])
    cols = list(null[(null>=10) & (null<=40)].index)
    data.iloc[:, cols] = data.iloc[:, cols].fillna(data.iloc[:, cols].median(), inplace=False)