Home > Enterprise >  Remove nan from list if some of the elements are data-frame
Remove nan from list if some of the elements are data-frame

Time:09-18

I have below list

import pandas as pd
import numpy as np
dat = pd.DataFrame({'name' : ['A', 'C', 'A', 'B', 'C'], 'val' : [1,2,1,2,4]})
List = [dat, np.nan, dat, np.nan, np.nan]
List

I want to retain only those elements where they are not nan.

There is a similar discussion in How can I remove Nan from list Python/NumPy, but it is only applicable if all elements are scalar.

Is there any way to remove nan if some elements are matrix/dataframe?

CodePudding user response:

You can do:

[x for x in List if isinstance(x, pd.DataFrame)]

If you insist on filtering on filter out nan only, then remember that it is a float instance, so:

[x for x in List if not(isinstance(x, float) and np.isnan(x))]

CodePudding user response:

I'd suggest

[np.isnan(i) if not isinstance(i, pd.DataFrame) else False for i in List]

It's probably slow since you have to check if the item is a DataFrame, but it works.

Edit: You could also do

[i == np.nan if not isinstance(i, pd.DataFrame) else False for i in List]

Not sure which version is faster.

  • Related