Python Pandas Dataframe,删除“无"是任何列中的值的所有行 [英] Python Pandas Dataframe, remove all rows where 'None' is the value in any column

查看:209
本文介绍了Python Pandas Dataframe,删除“无"是任何列中的值的所有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大数据框.创建时使用'None'作为无法计算数字的值(而不是'nan')

I have a large dataframe. When it was created 'None' was used as the value where a number could not be calculated (instead of 'nan')

如何删除任何列中包含无"的所有行?我虽然可以使用 df.dropna 并设置 na 的值,但我似乎无法做到.

How can I delete all rows that have 'None' in any of it's columns? I though I could use df.dropna and set the value of na, but I can't seem to be able to.

谢谢

我认为这是数据框的一个很好的表示:

I think this is a good representation of the dataframe:

temp = pd.DataFrame(data=[['str1','str2',2,3,5,6,76,8],['str3','str4',2,3,'None',6,76,8]])

推荐答案

设置
借用@MaxU的df

df = pd.DataFrame([
    [1, 2, 3],
    [4, None, 6],
    [None, 7, 8],
    [9, 10, 11]
], dtype=object)

解决方案
你可以只使用 pd.DataFrame.dropna 原样

df.dropna()

   0   1   2
0  1   2   3
3  9  10  11

<小时>

假设你有 None 这样的字符串 df

df = pd.DataFrame([
    [1, 2, 3],
    [4, 'None', 6],
    ['None', 7, 8],
    [9, 10, 11]
], dtype=object)

然后结合 dropnamask

df.mask(df.eq('None')).dropna()

   0   1   2
0  1   2   3
3  9  10  11

当你比较时,你可以确保整个数据帧都是object.

You can ensure that the entire dataframe is object when you compare with.

df.mask(df.astype(object).eq('None')).dropna()

   0   1   2
0  1   2   3
3  9  10  11

这篇关于Python Pandas Dataframe,删除“无"是任何列中的值的所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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