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?