pandas 数据框中的 Select 行,其中指定的列不全是 NaN
Select rows from pandas data frame where specified columns are not all NaN
我有一个 Pandas DataFrame 对象 data
,列 'a', 'b', 'c', ..., 'z'
我想select满足以下条件的所有行:列'b'
、'c'
、'g'
中的数据不是同时为NaN。我试过了:
new_data = data[not all(np.isnan(value) for value in data[['b', 'c', 'g']])]
但它不起作用 - 抛出错误:
Traceback (most recent call last):
File "<input>", line 1, in <module>`
File "<input>", line 1, in <genexpr>
TypeError: Not implemented for this type
我想 select 所有行,满足以下条件:列 'b'、'c'、'g' 中的数据不是 NaN同时
那么你可以使用dropna
:
new_data = data.dropna(how='all', subset=['b', 'c', 'g'])
使用 parameters:
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 DataFrame 对象 data
,列 'a', 'b', 'c', ..., 'z'
我想select满足以下条件的所有行:列'b'
、'c'
、'g'
中的数据不是同时为NaN。我试过了:
new_data = data[not all(np.isnan(value) for value in data[['b', 'c', 'g']])]
但它不起作用 - 抛出错误:
Traceback (most recent call last):
File "<input>", line 1, in <module>`
File "<input>", line 1, in <genexpr>
TypeError: Not implemented for this type
我想 select 所有行,满足以下条件:列 'b'、'c'、'g' 中的数据不是 NaN同时
那么你可以使用dropna
:
new_data = data.dropna(how='all', subset=['b', 'c', 'g'])
使用 parameters:
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