Remove row with null value from pandas data frame Remove row with null value from pandas data frame python python

Remove row with null value from pandas data frame


This should do the work:

df = df.dropna(how='any',axis=0) 

It will erase every row (axis=0) that has "any" Null value in it.

EXAMPLE:

#Recreate random DataFrame with Nan valuesdf = pd.DataFrame(index = pd.date_range('2017-01-01', '2017-01-10', freq='1d'))# Average speed in miles per hourdf['A'] = np.random.randint(low=198, high=205, size=len(df.index))df['B'] = np.random.random(size=len(df.index))*2#Create dummy NaN value on 2 cellsdf.iloc[2,1]=Nonedf.iloc[5,0]=Noneprint(df)                A         B2017-01-01  203.0  1.1752242017-01-02  199.0  1.3384742017-01-03  198.0       NaN2017-01-04  198.0  0.6523182017-01-05  199.0  1.5775772017-01-06    NaN  0.2348822017-01-07  203.0  1.7329082017-01-08  204.0  1.4731462017-01-09  198.0  1.1092612017-01-10  202.0  1.745309#Delete row with dummy valuedf = df.dropna(how='any',axis=0)print(df)                A         B2017-01-01  203.0  1.1752242017-01-02  199.0  1.3384742017-01-04  198.0  0.6523182017-01-05  199.0  1.5775772017-01-07  203.0  1.7329082017-01-08  204.0  1.4731462017-01-09  198.0  1.1092612017-01-10  202.0  1.745309

See the reference for further detail.

If everything is OK with your DataFrame, dropping NaNs should be as easy as that. If this is still not working, make sure you have the proper datatypes defined for your column (pd.to_numeric comes to mind...)


----clear null all colum-------

df = df.dropna(how='any',axis=0)

---if you want to clean NULL by based on 1 column.---

df[~df['B'].isnull()]

                A         B2017-01-01  203.0  1.1752242017-01-02  199.0  1.338474                              **2017-01-03  198.0       NaN** clean2017-01-04  198.0  0.6523182017-01-05  199.0  1.5775772017-01-06    NaN  0.2348822017-01-07  203.0  1.7329082017-01-08  204.0  1.4731462017-01-09  198.0  1.1092612017-01-10  202.0  1.745309

Please forgive any mistakes.


To remove all the null values dropna() method will be helpful

df.dropna(inplace=True)

To remove remove which contain null value of particular use this code

df.dropna(subset=['column_name_to_remove'], inplace=True)