'Remove Key = NaN within loop for Specific Column

I created a dictionary from a dataframe using a loop.

The key are ID's but some ID's are NaN from source.

How can I drop rows in the loop where the ID column (which is the key) equals to NaN?



Solution 1:[1]

You can drop NaN rows from the dataframe before turning it into a dictionary like this:

df = df[df['ID'].notna()]

and then proceede as before in creating your dictionary.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 WholesomeGhost