Pandas dropna() 函数不工作
Pandas dropna() function not working
我正在尝试从 pandas 数据帧中删除 NA 值。
我使用了 dropna()
(应该从数据框中删除所有 NA 行)。然而,它不起作用。
代码如下:
import pandas as pd
import numpy as np
prison_data = pd.read_csv('https://andrewshinsuke.me/docs/compas-scores-two-years.csv')
这就是您获取数据框的方式。如下所示,默认的 read_csv
方法确实将 NA 数据点转换为 np.nan
.
np.isnan(prison_data.head()['out_custody'][4])
Out[2]: True
方便的是,DF 的 head()
已经包含一个 NaN 值(在列 out_custody
中),所以打印 prison_data.head()
这个,你得到:
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
但是,运行 prison_data.dropna()
不会以任何方式更改数据帧。
prison_data.dropna()
np.isnan(prison_data.head()['out_custody'][4])
Out[3]: True
它不起作用,因为每行至少有一个 nan
df.dropna()
默认 returns 没有 NaN
值的新数据集。因此,您必须将其分配给变量
df = df.dropna()
如果你想让它就地修改df
,你必须明确指定
df.dropna(inplace= True)
我正在尝试从 pandas 数据帧中删除 NA 值。
我使用了 dropna()
(应该从数据框中删除所有 NA 行)。然而,它不起作用。
代码如下:
import pandas as pd
import numpy as np
prison_data = pd.read_csv('https://andrewshinsuke.me/docs/compas-scores-two-years.csv')
这就是您获取数据框的方式。如下所示,默认的 read_csv
方法确实将 NA 数据点转换为 np.nan
.
np.isnan(prison_data.head()['out_custody'][4])
Out[2]: True
方便的是,DF 的 head()
已经包含一个 NaN 值(在列 out_custody
中),所以打印 prison_data.head()
这个,你得到:
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
但是,运行 prison_data.dropna()
不会以任何方式更改数据帧。
prison_data.dropna()
np.isnan(prison_data.head()['out_custody'][4])
Out[3]: True
它不起作用,因为每行至少有一个 nan
df.dropna()
默认 returns 没有 NaN
值的新数据集。因此,您必须将其分配给变量
df = df.dropna()
如果你想让它就地修改df
,你必须明确指定
df.dropna(inplace= True)