1
I have the following doubt.
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
What I intend to do is:
- From column B I want to check in which row column B is 'NAN' and if so, I want to create another dataframe containing the same columns as the current one (df), but only with index rows 1 and 2 (in this case).
To better illustrate, the result should be:
df2
I initially tried using the command Loc
df2 = df.loc[:]
however, I could not reference how only seek the values np. Nan, there is some 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 but looks for blank lines in any column, how could change it to take a single column?
I don’t know much about numpy, but I found a question that might help you, https://stackoverflow.com/questions/6736590/fast-check-for-nan-in-numpy
– Junior Nascimento