Select rows from pandas data frame where specified columns are not all NaN

强颜欢笑 提交于 2019-12-11 10:09:18

问题


I have a Pandas DataFrame object data with columns 'a', 'b', 'c', ..., 'z'

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

回答1:


I want to select all rows, which qualify the following condition: data in columns 'b', 'c', 'g' is not NaN simultaneously.

Then you can use dropna:

new_data = data.dropna(how='all', subset=['b', 'c', 'g'])

using 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


来源:https://stackoverflow.com/questions/34311957/select-rows-from-pandas-data-frame-where-specified-columns-are-not-all-nan

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!