3

I have a pandas dataframe.

I want to collect/print the values of column A where column B is NaN.

Question How do I do this?

Edit Further: Say I have a set of columns (b,c,d). I want to select the values of column a if either b,c, or d is NaN.

(The trick for identifying NaNs is a bit different than simply "==" etc.)

Thank you

Jesper - jtk.eth
  • 7,026
  • 11
  • 36
  • 63

3 Answers3

3

It should be pretty straightforward:

In [10]: df
Out[10]:
     a  b  c
0  NaN  9  7
1  1.0  7  6
2  5.0  9  1
3  7.0  4  0
4  NaN  2  3
5  2.0  4  6
6  6.0  3  6
7  0.0  2  7
8  9.0  1  4
9  2.0  9  3

In [11]: df.loc[df['a'].isnull(), 'b']
Out[11]:
0    9
4    2
Name: b, dtype: int32

UPDATE:

In [166]: df
Out[166]:
     a    b  c
0  NaN  5.0  3
1  7.0  NaN  8
2  4.0  9.0  7
3  8.0  NaN  9
4  3.0  0.0  5
5  NaN  3.0  5
6  9.0  0.0  3
7  0.0  2.0  6
8  7.0  8.0  7
9  1.0  7.0  6


In [163]: df[['a','b']].isnull().any(axis=1)
Out[163]:
0     True
1     True
2    False
3     True
4    False
5     True
6    False
7    False
8    False
9    False
dtype: bool

In [164]: df.loc[df[['a','b']].isnull().any(axis=1)]
Out[164]:
     a    b  c
0  NaN  5.0  3
1  7.0  NaN  8
3  8.0  NaN  9
5  NaN  3.0  5

In [165]: df.loc[df[['a','b']].isnull().any(axis=1), 'c']
Out[165]:
0    3
1    8
3    9
5    5
Name: c, dtype: int32
MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419
0

You can also use np.isnan()

   df=       

   a     b 
 1 Nan   2 
 2 2     3 
 3 1     NaN

 for i in range(1,4):
      if np.isnan(df.loc[i,'a']):
            print(df.loc[i,'b'])
 out: 2
0

Perhaps try the .fillna() method to replace your NaNs. You can also index to a specific column you would like to address rather than the whole data frame.

Here is a link to the docs: DataFrame.fillna

In [7]: df
Out[7]: 
          0         1
0       NaN       NaN
1 -0.494375  0.570994
2       NaN       NaN
3  1.876360 -0.229738
4       NaN       NaN

In [8]: df.fillna(0)
Out[8]: 
          0         1
0  0.000000  0.000000
1 -0.494375  0.570994
2  0.000000  0.000000
3  1.876360 -0.229738
4  0.000000  0.000000