从 Pandas 数据框中选择指定列不全为 NaN 的行 [英] Select rows from pandas data frame where specified columns are not all NaN
本文介绍了从 Pandas 数据框中选择指定列不全为 NaN 的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 Pandas DataFrame 对象 data
列 'a', 'b', 'c', ..., 'z'
I have a Pandas DataFrame object data
with columns 'a', 'b', 'c', ..., 'z'
我想选择满足以下条件的所有行:'b'
、'c'
、'g'
列中的数据不是 NaN 同时.我试过了:
I want to select all rows which satisfy the following condition: data in columns 'b'
, 'c'
, 'g'
is not NaN simultaneously. I tried:
new_data = data[not all(np.isnan(value) for value in data[['b', 'c', 'g']])]
但它不起作用 - 引发错误:
but it didn't work - throws an error:
Traceback (most recent call last):
File "<input>", line 1, in <module>`
File "<input>", line 1, in <genexpr>
TypeError: Not implemented for this type
推荐答案
我想选择满足以下条件的所有行:列 'b'、'c'、'g' 中的数据不是 NaN同时.
然后就可以使用dropna
:
new_data = data.dropna(how='all', subset=['b', 'c', 'g'])
使用参数:
how : {'any', 'all'}
* any : if any NA values are present, drop that label
* all : if all values are NA, drop that label
subset : array-like
Labels along other axis to consider, e.g. if you are dropping rows
these would be a list of columns to include
这篇关于从 Pandas 数据框中选择指定列不全为 NaN 的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文