Python Forum

Full Version: Dropping rows with missing values
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I have this code below, I'm trying to drop rows with missing values, and I'm trying to use DataFrame.dropna(), But when I test below the rows were not dropped, please help. I have attached my data set as well.

import pandas as pd

recent_grads = pd.read_csv('recent-grads.csv')
print(recent_grads['Low_wage_jobs'].value_counts().sort_index().head())
print(len(recent_grads))

recent_grads = recent_grads.dropna()

print(recent_grads['Low_wage_jobs'].value_counts().sort_index().head())
print(len(recent_grads))
Are you sure that your data frame elements are nans? The following example shows that everything works fine:

>>> df = pd.DataFrame({'a': [pd.np.nan, 3, 4], 'b': [4, pd.np.nan, 8]})
>>> df
Output:
a b 0 NaN 4.0 1 3.0 NaN 2 4.0 8.0
>>> df.dropna()
Output:
a b 2 4.0 8.0
(Jul-27-2020, 05:34 AM)scidam Wrote: [ -> ]Are you sure that your data frame elements are nans? The following example shows that everything works fine:

>>> df = pd.DataFrame({'a': [pd.np.nan, 3, 4], 'b': [4, pd.np.nan, 8]})
>>> df
Output:
a b 0 NaN 4.0 1 3.0 NaN 2 4.0 8.0
>>> df.dropna()
Output:
a b 2 4.0 8.0

Thank you for this, I see where I made a mistake