1

I am trying to create a function for NaN handling. I would like to always assign the mean of the previous and preceding element for the NaN value.

My idea was something that looks like this

def na_handler(df):
    for i in range(1, len(df)):
         if i == NaN:
                i = ((i-1) + (i+1)) / 2

However this doesnt work, since I dont know how to access the elements in the df properly.

I somehow dont really get the basics of how accessing data in a df works. I have watched some Videos on data handling in python but I always get stuck when I try to solve a problem. If anyone has a good recomendation on where to learn data science with python I would be very thankful. Thanks for any help!

Pajul
  • 135
  • 6
  • Does this answer your question? [How can I check for NaN values?](https://stackoverflow.com/questions/944700/how-can-i-check-for-nan-values) – Green Cloak Guy Nov 16 '20 at 14:29

1 Answers1

3

You can use interpolation, which linearly fills the NaN values with the mean of the previous and next values.

So if it is a dataframe you are working with for example:

df.interpolate(method ='linear', limit_direction ='forward', inplace = True)

would do the trick.

apol96
  • 200
  • 12