Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I have the following code:
my_dataset={'item1':{'item11':0,'item12':'NaN','item13':2},'item2':{'item21':0,'item22':'NaN','item23':2}}
my_dataset_clean=my_dataset
for item in my_dataset:
my_dataset_clean[item] = {k: 0 for k in my_dataset[item] if isnan(my_dataset[item][k])}
I am getting this error:
my_dataset_clean[item] = {k: 0 for k in my_dataset[item] if
isnan(my_dataset[item][k])} TypeError: must be real number, not str
Any ideas on how to solve the issue? I want to replace NaN with 0s
You need to check 'NaN' as a string instead of use isnan (presumably np.isnan) which expects a numeric value. But there are a few other points worth noting:
You don't define what happens when your value is not 'NaN'. Without this you will lose the other items. See below for how you can define a ternary statement.
my_dataset_clean = my_dataset does not create a copy of your dictionary. You need to be explicit, e.g. my_dataset_clean = my_dataset.copy(), to do this. Otherwise you just have 2 variables pointing to the same dictionary.
You can use a dictionary comprehension to avoid having to make a copy of your dictionary.
Here's an example with a dictionary comprehension and ternary statement:
my_dataset_clean = {k: {k2: 0 if v2 == 'NaN' else v2 for k2, v2 in v.items()} \
for k, v in my_dataset.items()}
Here k, v refer to keys / values in the outer dictionary, and k2, v2 refer to keys / values in inner dictionaries.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.