Python Pandas:选择列值为null/None/nan的行 [英] Python pandas: selecting rows whose column value is null / None / nan

查看:688
本文介绍了Python Pandas:选择列值为null/None/nan的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何选择DataFrame的那些列中无值的行?

How do I select those rows of a DataFrame whose value in a column is none?

我已经将它们编码为np.nan,并且无法与此类型匹配.

I've coded these to np.nan and can't match against this type.

In [1]: import numpy as np

In [2]: import pandas as pd

In [3]: df = pd.DataFrame([[1, 2, 3], [3, 4, None]])

In [4]: df
Out[4]: 
   0  1    2
0  1  2  3.0
1  3  4  NaN

In [5]: df = df.fillna(np.nan)

In [6]: df
Out[6]: 
   0  1    2
0  1  2  3.0
1  3  4  NaN

In [7]: df.iloc[1][2]
Out[7]: nan

In [8]: df.iloc[1][2] == np.nan
Out[8]: False

In [9]: df[df[2] == None]
Out[9]: 
Empty DataFrame
Columns: [0, 1, 2]
Index: []

推荐答案

,您可以使用 查看全文

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