{'140508921-1183337': {'COVERAGE_PILLAR': nan,
'HARDWARE_PILLAR': nan,
'SECURITY_PILLAR': nan,
'SOFTWARE_PILLAR': nan},
'PSS_13240-1451364': {'COVERAGE_PILLAR': 10.0,
'HARDWARE_PILLAR': 25.0,
'SECURITY_PILLAR': 55.0,
'SOFTWARE_PILLAR': 10.0}
}
want to remove keys which has nan values . such as '140508921-1183337''should be removed as its keys/values pairs null
答案 0 :(得分:0)
If a
is the dictionary do this:
a={'140508921-1183337': {'COVERAGE_PILLAR': 'nan', 'HARDWARE_PILLAR': 'nan', 'SECURITY_PILLAR':'nan', 'SOFTWARE_PILLAR': 'nan'}, 'PSS_13240-1451364': {'COVERAGE_PILLAR': 10.0, 'HARDWARE_PILLAR': 25.0, 'SECURITY_PILLAR': 55.0, 'SOFTWARE_PILLAR': 10.0}}
def remove_keys_with_nan(item):
if not hasattr(item, 'items'):
return item
else:
d = {key: remove_keys_with_nan(value) for key, value in item.iteritems() if value is not 'nan'}
return dict((k, v) for k, v in d.iteritems() if v)
a = remove_keys_with_nan(a)
print(a)
Result:
{'PSS_13240-1451364': {'COVERAGE_PILLAR': 10.0, 'HARDWARE_PILLAR': 25.0, 'SOFTWARE_PILLAR': 10.0, 'SECURITY_PILLAR': 55.0}}
EDIT-new answer to your new question
use:
import numpy as np
nan = np.nan
a={'140508921-1183337': {'COVERAGE_PILLAR': nan, 'HARDWARE_PILLAR': nan, 'SECURITY_PILLAR': nan, 'SOFTWARE_PILLAR': nan}, 'PSS_13240-1451364': {'COVERAGE_PILLAR': 10.0, 'HARDWARE_PILLAR': 25.0, 'SECURITY_PILLAR': 55.0, 'SOFTWARE_PILLAR': 10.0}}
and then the same code as previously.