Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove nan values from a dict in python

I am trying to remove keys with nan values from a dictionary formed from pandas using python. Is there a way I can achieve this. Here is a sample of my dictionary:

{'id': 1, 'internal_id': '1904', 'first_scraping_time': '2020-04-17 12:44:59.0', 'first_scraping_date': '2020-04-17', 'last_scraping_time': '2020-06-20 03:08:47.0', 'last_scraping_date': '2020-06-20', 'is_active': 1,'flags': nan, 'phone': nan,'size': 60.0, 'available': '20-06-2020', 'timeframe': nan, 'teaser': nan, 'remarks': nan, 'rent': 4984.0, 'rooms': '3', 'downpayment': nan, 'deposit': '14952', 'expenses': 600.0, 'expenses_tv': nan, 'expenses_improvements': nan, 'expenses_misc': nan, 'prepaid_rent': '4984', 'pets': nan, 'furnished': nan, 'residence_duty': nan, 'precision': nan, 'nearby_cities': nan,'type_dwelling': nan, 'type_tenants': nan, 'task_id': '614b8fc2-409c-403a-9650-05939e8a89c7'}

Thank you!

like image 666
Derrick Omanwa Avatar asked Sep 01 '25 10:09

Derrick Omanwa


1 Answers

nan is a tricky object to work with because it doesn't equal (or even necessarily share object identity) with anything, including itself.

You can use math.isnan to test for it:

import math
new = {key: value for (key, value) in old.items() if not math.isnan(value)}
like image 73
L3viathan Avatar answered Sep 04 '25 02:09

L3viathan