Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
727 views
in Technique[技术] by (71.8m points)

python - Pandas dropna() function not working

I am trying to drop NA values from a pandas dataframe.

I have used dropna() (which should drop all NA rows from the dataframe). Yet, it does not work.

Here is the code:

import pandas as pd
import numpy as np
prison_data = pd.read_csv('https://andrewshinsuke.me/docs/compas-scores-two-years.csv')

That's how you get the data frame. As the following shows, the default read_csv method does indeed convert the NA data points to np.nan.

np.isnan(prison_data.head()['out_custody'][4])

Out[2]: True

Conveniently, the head() of the DF already contains a NaN values (in the column out_custody), so printing prison_data.head() this, you get:

   id                name   first         last compas_screening_date   sex  

0   1    miguel hernandez  miguel    hernandez            2013-08-14  Male
1   3         kevon dixon   kevon        dixon            2013-01-27  Male
2   4            ed philo      ed        philo            2013-04-14  Male
3   5         marcu brown   marcu        brown            2013-01-13  Male
4   6  bouthy pierrelouis  bouthy  pierrelouis            2013-03-26  Male

      dob  age          age_cat              race      ...        
0  1947-04-18   69  Greater than 45             Other      ...
1  1982-01-22   34          25 - 45  African-American      ...
2  1991-05-14   24     Less than 25  African-American      ...
3  1993-01-21   23     Less than 25  African-American      ...
4  1973-01-22   43          25 - 45             Other      ...

   v_decile_score  v_score_text  v_screening_date  in_custody  out_custody  

0               1           Low        2013-08-14  2014-07-07   2014-07-14
1               1           Low        2013-01-27  2013-01-26   2013-02-05
2               3           Low        2013-04-14  2013-06-16   2013-06-16
3               6        Medium        2013-01-13         NaN          NaN
4               1           Low        2013-03-26         NaN          NaN

priors_count.1 start   end event two_year_recid
0               0     0   327     0              0
1               0     9   159     1              1
2               4     0    63     0              1
3               1     0  1174     0              0
4               2     0  1102     0              0

However, running prison_data.dropna() does not change the dataframe in any way.

prison_data.dropna()
np.isnan(prison_data.head()['out_custody'][4])


Out[3]: True
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

df.dropna() by default returns a new dataset without NaN values. So, you have to assign it to the variable

df = df.dropna()

if you want it to modify the df inplace, you have to explicitly specify

df.dropna(inplace= True)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...