Python Forum

Full Version: if nan -> find value in cell above Pandas
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi guys,

I have excel file like this:
Sometext_here
nan
nan
nan
nan
nan
nan
nan
TotallyNewThing
nan
nan
nan
How can i replace nan values? I want to replace them with VALUE above them if not nan
I am struggling with this :(

I have tried this:
for index, value in enumerate(survey):
    if value == '' or (isinstance(value, float) and  math.isnan(value)):
        value = value[index-1]
    print(value)
TypeError: 'float' object is not subscriptable
As the error says, "value" is a float and float[5] is not allowed. But you have something that is "subscriptable". Hint it is in your for loop inside the enumerate().
(Mar-27-2020, 02:47 PM)deanhystad Wrote: [ -> ]As the error says, "value" is a float and float[5] is not allowed. But you have something that is "subscriptable". Hint it is in your for loop inside the enumerate().
Thank you :P
ok i think i got it
for index, value in enumerate(survey):
    while value == '' or (isinstance(value, float) and  math.isnan(value)):
        value = survey[index]
        index = index - 1   
    print(value)
There is not enough information for me to understand what you are asking. The code is incomplete, so I cannot run the program and see what happens, and your question is lacking detail. Wat does "cannot make proper loop through" mean. Are you getting an error? Does the code run but it is not doing what you want? I know the latter is true because:
value = survey[index-1]
Is not going to replace a NAN in survey.