k-NN imputation k-NN imputation

You have set up a DIY home weather station, which uses a bunch of different sensors (for humidity, temperature, etc), for a school project. For the past 4 days you have been collecting the data directly into your computer, but today you realised that some data is missing! This is probably due to sensor failure, but you don't want to spend another 4 days measuring because you will miss the deadline for the project! So you decide to use a value imputation method to fill in the missing data with approximate values.

The data you have collected is inside a Pandas DataFrame and the cells containg the numpy.nan value are the missing data. For all numpy.nan values in data, you must find the k Nearest Neighbors and then replace the numpy.nan with the average of the found neighbors.

Input: A Pandas DataFrame with the data, the number of k neighbors.

Output: A Pandas DataFrame with the imputed data.