Reputation: 1895
Remove NaN values from the nested dictionary in python. My nested dictionary looks like below:
my_dict = {'abc':{'a':0.987, 'b':0.765, 'c': numpy.nan}, 'mda':{'a':0.145, 'b':0.584, 'e':numpy.nan}, 'fop':{'a':0.145, 'b': numpy.nan, 'c':0.485}}
I want to drop Nan values from the nested dictioanry, expected result looks like:
my_dict = {'abc':{'a':0.987, 'b':0.765}, 'mda':{'a':0.145, 'b':0.584}, 'fop':{'a':0.145, 'c':0.485}}
i have written below code to remove Nan's from the nested dictionary:
def remove_nan_from_dict(my_dict):
new_dict = {}
original_dict = collections.defaultdict()
from math import isnan
for k, v in my_dict.items():
for k1, v1 in v.items():
print(v1)
if not isnan(v1):
new_dict[k1] = v1
print(new_dict)
original_dict[k] = new_dict
# print(original_dict.items())
return original_dict
But getting below dictionary:
defaultdict(None, {'abc': {'a': 0.145, 'b': 0.584, 'c': 0.485}, 'mda': {'a': 0.145, 'b': 0.584, 'c': 0.485}, 'fop': {'a': 0.145, 'b': 0.584, 'c': 0.485}})
Upvotes: 0
Views: 1275
Reputation: 559
I had a 3 level nested-dictionary so I used the following:
import numpy as np
for k, v in your_dict.items():
for kk, vv in your_dict[k].items():
for kkk, vvv in your_dict[k][kk].items():
get_key = your_dict[k][kk][kkk]
print(get_key)
for kkkk, vvvv in get_key.copy().items():
if np.isnan(vvvv):
del get_key[kkkk]
Upvotes: 0
Reputation: 13106
You can just use a dict comprehension here and check if the value is an np.nan
:
new_dict = {k: {a: b for a, b in v.items() if not np.isnan(b)} for k, v in my_dict.items()}
Upvotes: 1
Reputation: 740
You can use 2 simple loops like this:
my_dict = {'abc':{'a':0.987, 'b':0.765, 'c': numpy.nan}, 'mda':{'a':0.145, 'b':0.584, 'e':numpy.nan}, 'fop':{'a':0.145, 'b': numpy.nan, 'c':0.485}}
for items in my_dict:
get_key = my_dict[items]
for key, values in get_key.copy().items():
if numpy.isnan(values):
del get_key[key]
my_dict
> {'abc': {'a': 0.987, 'b': 0.765},
'mda': {'a': 0.145, 'b': 0.584},
'fop': {'a': 0.145, 'c': 0.485}}
Upvotes: 1