繁体   English   中英

graphlab查找具有至少一个None值的所有列

[英]graphlab find all the columns that has at least one None value

如何在SFrame中查找其中至少包含一个None值的所有列? 一种方法是遍历每一列,并检查该列中是否有值是None。 有更好的方法来完成这项工作吗?

要在SFrame查找None值, SFrame使用SArray方法num_missingdoc )。

>>> col_w_none = [col for col in sf.column_names() if sf[col].num_missing()>0]

>>> sf = gl.SFrame({'foo':[1,2,3,4], 'bar':[1,None,3,4]})
>>> print sf
+------+-----+
| bar  | foo |
+------+-----+
|  1   |  1  |
| None |  2  |
|  3   |  3  |
|  4   |  4  |
+------+-----+
[4 rows x 2 columns]
>>> print [col for col in sf.column_names() if sf[col].num_missing()>0]
['bar']

注意事项

  • 这不是最佳选择,因为它不会停止在第一个None值上进行迭代。
  • 它不会检测到NaN和空字符串。
>>> sf = gl.SFrame({'foo':[1,2,3,4], 'bar':[1,None,3,4], 'baz':[1,2,float('nan'),4], 'qux':['spam', '', 'ham', 'eggs']} )
>>> print sf
+------+-----+-----+------+
| bar  | baz | foo | qux  |
+------+-----+-----+------+
|  1   | 1.0 |  1  | spam |
| None | 2.0 |  2  |      |
|  3   | nan |  3  | ham  |
|  4   | 4.0 |  4  | eggs |
+------+-----+-----+------+
[4 rows x 4 columns]
>>> print [col for col in sf.column_names() if sf[col].num_missing()>0]
['bar']

这是熊猫的解决方案:

In [50]: df
Out[50]:
   keys  values
0     1     1.0
1     2     2.0
2     2     3.0
3     3     4.0
4     3     5.0
5     3     NaN
6     3     7.0

In [51]: df.columns.to_series()[df.isnull().any()]
Out[51]:
values    values
dtype: object

In [52]: df.columns.to_series()[df.isnull().any()].tolist()
Out[52]: ['values']

说明:

In [53]: df.isnull().any()
Out[53]:
keys      False
values     True
dtype: bool

您可以使用isull

pd.isnull(df).sum() > 0

例:

df = pd.DataFrame({'col1':['A', 'A', 'B','B'], 'col2': ['B','B','C','C'], 'col3': ['C','C','A','A'], 'col4': [11,12,13,np.nan], 'col5': [30,10,14,91]})
df
    col1 col2 col3  col4 col5
0   A   B   C   11.0    30
1   A   B   C   12.0    10
2   B   C   A   13.0    14
3   B   C   A   NaN     91

pd.isnull(df).sum() > 0

col1    False
col2    False
col3    False
col4     True
col5    False
dtype: bool

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM