0

I have a dataset with missing values, I determined the best way to approach this problem is to find the median of the row and replace the NaN values with the median value. However, the code runs but values are not replaced.

medianinf= inflation_data.iloc[:, 4:-1].median(axis=1) filledinfdata= inflation_data.replace(to_replace= np.nan, value= medianinf) 
medianinf= inflation_data.iloc[:, 4:-1].median(axis=1) filled_inf_data=inflation_data.fillna(medianinf) 

I tried both of these code, a median value is produced of each row but the Nan Values in the dataset are not getting replaced

3
  • 4
    Does this answer your question? pandas DataFrame: replace nan values with average of columns. Except you'll need to replace df.mean with df.median Commented Sep 26, 2023 at 14:13
  • Have you tried using inplace=True in the original dataframe? Try doing inflation_data.fillna(medianinf, inplace=True) after your second piece of code, without assigning it to "filled_inf_data". Commented Sep 26, 2023 at 14:56
  • Recommend reading minimal reproducible example: need minimal yet representative input data Commented Sep 28, 2023 at 22:30

1 Answer 1

0

For your specified columns...

for i in range(len(inflation_data)): inflation_data.iloc[i] = inflation_data.iloc[i].fillna(inflation_data.iloc[i,4:-1].median()) 

If all columns are numerical...

for i in range(len(inflation_data)): inflation_data.iloc[i] = inflation_data.iloc[i].fillna(inflation_data.iloc[i].median()) 
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, Loop so we can calculate the median value for each row as specified in question and then impute for NA values. inflation_data.iloc[i] returns the row as a series.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.