I would like to remove the NaNs from my dictionary.

my_dict = {'House': ['has keys',
  'check lights',
  nan,
  nan,
  nan],
 'The Office': ['reading',
  nan,
  nan,
  nan,
  'coffee breaks']}

I believe the NaNs are floats, not strings. I have tried:

import math

my_dict['House'] = [
    x
    for x in dict2['House']
    if not (isinstance(x, float) and math.isnan(x))
]

And I get:

my_dict = {'House': ['has keys',
  'check lights',],
 'The Office': ['reading',
  nan,
  nan,
  nan,
  'coffee breaks']}

I would like it to look like the following, but I dont know how to get my for loop to loop through all keys instead of just House:

my_dict = {'House': ['has keys',
  'check lights'],
 'The Office': ['reading',
  'coffee breaks']}

4 Answers

1
Óscar López On

This should work, it'll filter all the values in the dictionary, removing NaN numbers:

{ k: [x for x in v if not isinstance(x, float) or not math.isnan(x)] for k, v in my_dict.items() }

This is the result:

{'House': ['has keys', 'check lights'],
 'The Office': ['reading', 'coffee breaks']}
0
Nullman On

you are on the correct track, but you only check 'house', you need to apply your logic to all your keys:

import math
for tv_show in my_dict:
    my_dict[tv_show] = [
        x
        for x in dict2[tv_show]
        if not (isinstance(x, float) and math.isnan(x))
    ]
0
KenHBS On

You could also go the other way around and only retain string values (as long as you ONLY want string values, of course):

>>> for k, v in my_dict.items():
>>>     my_dict[k] = [val for val in v if isinstance(val, str)]

>>> my_dict
# {'House': ['has keys', 'check lights'], 'The Office': ['reading', 'coffee breaks']}

Even though this can also be achieved in a dictionary comprehension, I think the logic is a bit dense in this case for a one-liner, however, it would look like this:

>>> {k: [val for val in v if isinstance(val, str)] for k, v in my_dict.items()}
1
anky_91 On

Since you have tagged pandas, you can do something like:

print(df)

          House     The Office
0      has keys        reading
1  check lights            NaN
2           NaN            NaN
3           NaN            NaN
4           NaN  coffee breaks

df_new=df.apply(lambda x: pd.Series(x.dropna().values))
print(df_new)

          House     The Office
0      has keys        reading
1  check lights  coffee breaks