1

I have a data frame created with Pandas that contains numbers. I need to check if the values that I extract from this data frame are nulls or zeros. So I am trying the following:

a = df.ix[[0], ['Column Title']].values  
if a != 0 or not math.isnan(float(a)):
    print "It is neither a zero nor null"

While it does appear to work, sometimes I get the following error:

TypeError: don't know how to convert scalar number to float

What am I doing wrong?

6
  • 1
    Please include the value of a that causes the error. Commented Jul 25, 2017 at 0:51
  • 2
    That looks like an XY problem, please post what you want to accomplish. Commented Jul 25, 2017 at 0:53
  • I'm incredibly new to pandas, so I could be completely wrong here, but to extract the not null values couldn't you do df[df['Column Title'] is not None]? Commented Jul 25, 2017 at 0:58
  • 2
    @CoryMadden null values are represented as np.nan by default in pandas Commented Jul 25, 2017 at 1:02
  • math.isnan(np.nan) == np.nan Commented Jul 25, 2017 at 1:05

1 Answer 1

6

your code to extract a single value from a series will return list of list format with a single value:

For Example: [[1]]

so try changing your code

 a = df.ix[[0], ['Column Title']].values 

to

 a = df.ix[0, 'Column Title']

then try

math.isnan(float(a))

this will work!!

Sign up to request clarification or add additional context in comments.

1 Comment

ix is deprecated in 0.20. Use iloc for index based addressing or loc otherwise.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.