Home > database >  Pandas dropna function is returning original dataframe despite me setting a threshold. How may I res
Pandas dropna function is returning original dataframe despite me setting a threshold. How may I res

Time:02-22

Not sure why dropna() isn't working... 'data' is a preset pandas dataframe with 5 named columns with all values like integers or NA Part of my project is to ask the user to input their own threshold by the way

threshold = input("What is your threshold for NAs in a given row?")
try:
   num = int(threshold)
except:
   print("Please enter a number or leave blank")
   if num < 0:
      print("Please enter a positive integer")
   elif num >= 0:
      data = data.dropna(axis = 0, thresh=num, inplace=True)
      print(data)
                
return data

enter image description here

CodePudding user response:

Given the indentation in your code, the only place where you actually end up running dropna is in the except: block if int(threshold) fails. :) That's why you always just return the original data.

You're missing the except: block's else: block:

threshold = input("What is your threshold for NAs in a given row?")
try:
    num = int(threshold)
except:
    print("Please enter a number or leave blank")
else:  # <- this!
    if num < 0:
        print("Please enter a positive integer")
    else:
        data = data.dropna(axis=0, thresh=num, inplace=True)
        print(data)

return data

Of course it would be better to just fail altogether and not return unmodified data.

def input_positive_integer(prompt):
    while True:
        try:
            threshold = int(input(prompt))
            assert threshold > 0
            return threshold
        except Exception:
            print("Invalid input.")
            
threshold = input_positive_integer("What is your threshold for NAs in a given row?")
data.dropna(axis=0, thresh=threshold, inplace=True)  # inplace modification

CodePudding user response:

If you don't post the error message it's very unclear what is "not working".

However, I notice at least that you have inplace=True and are also reassigning the result. Simply set inplace to False will likely fix the problem.

Alternatively, you could not reassign the result and actually do it inplace.

  • Related