2

I have this dictionary:

dict_new = 
{'extracted_layout': [nan, nan, nan, nan, nan, nan, nan, nan, nan, 'shyamanna layout', nan, nan, nan, nan, 'm t s layout', nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, 'green glen layout', nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, 'h s r layout', nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, 'vikas layout', 'annaiah reddy layout', nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, 'andhra muniyappa layout', nan, nan, nan, nan, nan, 'lake city layout', nan, 'h s r layout'}

It has one key, extracted_layout and its values are in a list that is stuffed with nans . How do I get rid of them?

falsetru
  • 357,413
  • 63
  • 732
  • 636
Hypothetical Ninja
  • 3,920
  • 13
  • 49
  • 75
  • please explain the reason for a downvote , is my question a duplicate ? or not specific? – Hypothetical Ninja Sep 05 '14 at 11:26
  • That doesn't make any sense ?? JUst downvote because the question is too small , really sad.. – Hypothetical Ninja Sep 05 '14 at 11:31
  • your comment is just an assumption ("because the question is too small") and probably not correct. The downvotes aren't from me but your question doesn't show any research effort. It doesn't say what you have tried so far. Thus your question is basically, "please do this for me". That's not liked by many. – Unapiedra Sep 05 '14 at 11:41
  • possible duplicate of [Is there a simple way to delete a list element by value in python?](http://stackoverflow.com/questions/2793324/is-there-a-simple-way-to-delete-a-list-element-by-value-in-python) – Kevin Sep 05 '14 at 11:42
  • It would be better if you specify the expected output, and what `nan`s are. – falsetru Sep 05 '14 at 11:50
  • 1
    The reason for downvote is probably due to lack of reason for question, what do you want to do this for? What is your required/expected output? What have you tried? Why didn't that work? – Joe Smart Sep 05 '14 at 13:00

2 Answers2

5

If nan is float nan, use math.isnan to filter it out:

>>> import math
>>> nan = float('nan')
>>> nan
nan
>>> math.isnan(nan)
True
>>> math.isnan(1)
False

import math

dict_new['extracted_layout'] = [
    x
    for x in dict_new['extracted_layout']
    if not (isinstance(x, float) and math.isnan(x))
]
falsetru
  • 357,413
  • 63
  • 732
  • 636
0
list_dict = [{"year": 1916, "age": 10},
        {"year": 1826, "age": 'NaN'},
        {"year": 'NaN', "age": 7}]
for my_dict in list_dict:
    for k in my_dict:
        if isnan(float(my_dict[k])):
            my_dict[k] = 0
Yagmur SAHIN
  • 277
  • 3
  • 3