Python中文
首页
教程
问答
标签
搜索
登录
注册
Pandas dropna()函数不工作
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我试图从pandas数据框中删除NA值。</p> <p>我使用了<code>dropna()</code>(它应该从数据帧中删除所有NA行)。然而,它不起作用。</p> <p>代码如下:</p> <pre><code>import pandas as pd import numpy as np prison_data = pd.read_csv('https://andrewshinsuke.me/docs/compas-scores-two-years.csv') </code></pre> <p>这就是获取数据帧的方法。如下所示,默认的<code>read_csv</code>方法确实将NA数据点转换为<code>np.nan</code>。</p> <pre><code>np.isnan(prison_data.head()['out_custody'][4]) Out[2]: True </code></pre> <p>方便的是,DF的<code>head()</code>已经包含一个NaN值(在<code>out_custody</code>列中),所以打印<code>prison_data.head()</code>这个,您将得到:</p> <pre><code> 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 </code></pre> <p>但是,运行<code>prison_data.dropna()</code>不会以任何方式更改数据帧。</p> <pre><code>prison_data.dropna() np.isnan(prison_data.head()['out_custody'][4]) Out[3]: True </code></pre>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>默认情况下,<code>df.dropna()</code>返回不带<code>NaN</code>值的新数据集。所以,你必须把它赋给变量</p> <pre><code>df = df.dropna() </code></pre> <p>如果希望它修改<code>df</code>位置,则必须显式指定</p> <pre><code>df.dropna(inplace= True) </code></pre>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
尽管Python中的所有内容都是引用,为什么Python导师在没有指针的列表中绘制字符串和整数?
7 回答
尽管python中的表达式为false,但循环仍在运行
2 回答
尽管python代码正确,但从nifi ExecuteScript处理器获取语法错误
10 回答
尽管Python在Neovim中工作得很好,但插件不能识别Neovim中的Python主机
8 回答
尽管python字典包含了大量的条目,但它并没有增长
3 回答
尽管python说模块存在,为什么我会得到这个消息?
9 回答
尽管setuptools和控制盘是最新的,但无法识别singleversionexternallymanaged
8 回答
尽管stdout和stderr重定向,但未捕获错误消息
10 回答
尽管Tensorboard的事件太大,但Tensorboard的步骤太少了
9 回答
尽管tkinter上的变量已更改,但显示未更改
10 回答
尽管try/except使用Python进行单元测试时出现断言错误
1 回答
尽管URL是sam,但仍会抛出“达到最大重定向”
2 回答
尽管url有效,Pandas仍读取url的\u csv错误
2 回答
尽管while中存在时间延迟,但LINUX线程的CPU利用率为100%(1)
10 回答
尽管x0在范围内,Scipy优化仍会引发ValueError
5 回答
尽管xpath正确,但使用selenium单击链接仍不起作用
10 回答
尽管下载了ffmpeg并设置了路径变量python,但没有后端错误
4 回答
尽管下载了i,但找不到型号“fr”
7 回答
尽管下载了plotnine包,但未获取名为“plotnine”的模块时出错
3 回答
尽管为所有行指定了权重,网格(0)仍不起作用
1 回答