Copy part of the Dataframe where column is Null or NaN

1

I have the following question.

I have the following sample dataframe:

import pandas as pd
df = pd.DataFrame({'A' : [4,5,13,18], 'B' : [10,np.nan,np.nan,40], 'C' : [np.nan,50,25,np.nan], 'D' : [-30,-50,10,16], 'E' : [-40,-50,7,12]})

Df

WhatIwanttodois:

  • FromcolumnB,IwanttocheckinwhichrowcolumnBis'NAN'andifso,Iwanttocreateanotherdataframecontainingthesamecolumnsofthecurrent(df),butonlywiththeindexlines1and2(inthiscase).

Forabetterexample,theresultshouldbe:

df2

I tried initially to use the loc

df2 = df.loc[:]

However, I could not reference how to only fetch the np.Nan values, is there any way to do this?

I tested with the pandas null fields to see the result.

    import pandas as pd
df = pd.DataFrame({'A' : [4,5,13,18], 'B' : [10,'','',40], 'C' : ['',50,25,''], 'D' : [-30,-50,10,16], 'E' : [-40,-50,7,12]})

And using the syntax:

df2 = df[pd.isnull(df).any(axis=1)]

This command works, however, searching for blank lines in any column, how could you change it to get a single column?

    
asked by anonymous 22.11.2018 / 20:13

1 answer

1

You can use one of three options:

op_a = df[df['B'].isnull()] # mesmo resultado com: isna()
# ou
op_b = df.loc[df['B'].isnull()] # mesmo resultado com: isna()
# ou
op_c = df.query('B != B')

Both will have the output:

   A    B     C   D   E
1  5  NaN  50.0 -50 -50

DataFrame used as an example:

{
  'A' : [4,5,13,18],
  'B' : [10,np.nan,'',40],
  'C' : [np.nan,50,25,np.nan],
  'D' : [-30,-50,10,16],
  'E' : [-40,-50,7,12]
}
  

You can be seen working at repl.it

    
23.11.2018 / 06:20