Python Forum
Dropping rows with missing values
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dropping rows with missing values
#1
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))
Reply
#2
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
Reply
#3
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,602 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  Calculate next rows based on previous values of array divon 0 1,716 Nov-23-2021, 04:44 AM
Last Post: divon
  Pandas DataFrame combine rows by column value, where Date Rows are NULL rhat398 0 2,083 May-04-2021, 10:51 PM
Last Post: rhat398
  Mouse lib keeps dropping same error MLGpotato 7 3,053 Apr-14-2021, 04:08 PM
Last Post: bowlofred
  Indexing [::-1] to Reverse ALL 2D Array Rows, ALL 3D, 4D Array Columns & Rows Python Jeremy7 8 6,963 Mar-02-2021, 01:54 AM
Last Post: Jeremy7
  Selecting rows that contain certain values using two alternatives tgottsc1 1 1,551 Jan-24-2021, 08:34 PM
Last Post: nealc
  How to generate rows based on values in a column to fill missing values codesmatter 1 2,095 Oct-31-2020, 12:05 AM
Last Post: Larz60+
  Find only the rows containing null values Bhavika 2 2,403 Jun-10-2020, 01:25 PM
Last Post: Bhavika
  Do Calculation between Rows based on Column values - Pandas Dataframe ahmedwaqas92 0 2,111 Jan-28-2020, 07:06 AM
Last Post: ahmedwaqas92
  How can I compare Python XML-Files and add missing values from one to another kirat 2 2,628 Aug-30-2019, 12:17 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020