如何删除 pandas 数据框中的行? [英] How to delete rows in a pandas dataframe?

查看:65
本文介绍了如何删除 pandas 数据框中的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个熊猫数据框,它实际上是一个 Excel 电子表格:

I have this pandas dataframe which is actually a excel spreadsheet:

    Unnamed: 0  Date    Num     Company     Link    ID
0   NaN     1990-11-15  131231  apple...    http://www.example.com/201611141492/xellia...   290834
1   NaN     1990-10-22  1231    microsoft http://www.example.com/news/arnsno...     NaN
2   NaN     2011-10-20  123     apple   http://www.example.com/ator...  209384
3   NaN     2013-10-27  123     apple...    http://example.com/sections/th-shots/2016/...   098
4   NaN     1990-10-26  123     google  http://www.example.net/business/Drugmak...  098098
5   NaN     1990-10-18  1231    google...   http://example.com/news/va-rece...  NaN
6   NaN     2011-04-26  546     amazon...   http://www.example.com/news/home/20160425...    9809

我想删除 ID 列中包含 NaN 的所有行并重新索引索引虚列":

I would like to remove all the rows that have NaN in the ID column and reindex the "index imaginary column":

    Unnamed: 0  Date    Num     Company     Link    ID
0   NaN     1990-11-15  131231  apple...    http://www.example.com/201611141492/xellia...   290834
1   NaN     2011-10-20  123     apple   http://www.example.com/ator...  209384
2   NaN     2013-10-27  123     apple...    http://example.com/sections/th-shots/2016/...   098
3   NaN     1990-10-26  123     google  http://www.example.net/business/Drugmak...  098098
4   NaN     2011-04-26  546     amazon...   http://www.example.com/news/home/20160425...    9809

我知道这可以按如下方式完成:

I know that this can be done as follows:

df = df['ID'].dropna()

df[df.ID != np.nan]

df = df[np.isfinite(df['ID'])]

TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

df[df.ID()]

或者:

df[df.ID != '']

然后:

df.reset_index(drop=True, inplace=True)

然而,它并没有去除ID中的NaN.我正在获取以前的数据框.

However, It didnt removed the NaN in ID. I am getting the former dataframe.

更新

在:

df['ID'].values

出:

array([ '....A lot of text....',
       nan,
       "A lot of text...",
       "More text",
       'text from the site',
       nan,
       "text from the site"], dtype=object)

推荐答案

试试这个

df = df[df.ID != 'nan']

这篇关于如何删除 pandas 数据框中的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆