Q:

Is there a pythonic way to replace nan values from dictionary?

I want to replace nan values from my dictionary. 
For example, sometimes my dictionary looks like:
{'mean': nan, 'std': nan, 'median': nan, 'sum': 0, 'average_per_day': 0.0, 'freq': 0}

Now I'm doing it like this:
for k, v in stats_record.items():
    if math.isnan(v): 
        stats_record[k] = 0

Is there a more pythonic way to replace nan values from dictionary?

A:

Dict-comprehension can be handy here.
import numpy as np

e = {'mean': np.nan, 'std': np.nan, 'median': np.nan, 'sum': 0, 'average_per_day': 0.0, 'freq': 0}

e = {k:v if not np.isnan(v) else 0 for k,v in e.items() }
print(e)

Output:
{'average_per_day': 0.0, 'sum': 0, 'freq': 0, 'median': 0, 'std': 0, 'mean': 0}

