Python Forum
if nan -> find value in cell above Pandas
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
if nan -> find value in cell above Pandas
#1
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
Reply
#2
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().
Reply
#3
(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)
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pandas pivot table: How to find count for each group in Index and Column JaneTan 0 3,348 Oct-23-2021, 04:35 AM
Last Post: JaneTan
  Pandas find the most often rows dervast 1 1,918 Jun-07-2019, 01:55 PM
Last Post: ichabod801
  find cell value with matching regular expression of a row in excel file hruday 4 30,955 Jul-05-2017, 01:02 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020