Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas: Locate/Select records where a column has list of nan

I have a dataset where few of my columns have lists of float as their value for a record. I'm trying to locate/select all those records where say in my dataframe df for column x1 the records have [nan, nan]

        x1            x2
0 [21.25, 25.45]   string1
1 [45.25, 85.45]   string2 
2   [nan, nan]     string3
3 [96.25, 79.45]   string4
4 [12.25, 65.45]   string5
5   [nan, nan]     string6
6   [nan, nan]     string7

I want the result:

        x1            x2
2   [nan, nan]     string3
5   [nan, nan]     string6
6   [nan, nan]     string7

I know the way to do it if there are no lists just float values for each record, df.loc[df['x1'].isnull()]

But I can't seem to figure out how to do it if it's a list.

like image 852
user9996043 Avatar asked Jan 25 '26 17:01

user9996043


1 Answers

Another method:

print(df[df['x1'].apply(lambda x: np.isnan(x).all())])

Prints:

           x1       x2
2  [nan, nan]  string3
5  [nan, nan]  string6
6  [nan, nan]  string7
like image 83
Andrej Kesely Avatar answered Jan 27 '26 05:01

Andrej Kesely



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!