On my own I found a way to drop nan rows from a pandas dataframe. Given a dataframe dat
with column x
which contains nan values,is there a more elegant way to do drop each row of dat
which has a nan value in the x
column?
dat = dat[np.logical_not(np.isnan(dat.x))]
dat = dat.reset_index(drop=True)
To expand Hitesh's answer if you want to drop rows where 'x' specifically is nan, you can use the subset parameter. His answer will drop rows where other columns have nans as well
dat.dropna(subset=['x'])
Just in case commands in previous answers doesn't work,
Try this:
dat.dropna(subset=['x'], inplace = True)
bool_series=pd.notnull(dat["x"])
dat=dat[bool_series]
To remove rows based on Nan value of particular column:
d= pd.DataFrame([[2,3],[4,None]]) #creating data frame
d
Output:
0 1
0 2 3.0
1 4 NaN
d = d[np.isfinite(d[1])] #Select rows where value of 1st column is not nan
d
Output:
0 1
0 2 3.0
Source: Stackoverflow.com