【问题标题】:Select rows from pandas data frame where specified columns are not all NaN从 Pandas 数据框中选择特定列并非全部为 NaN 的行
【发布时间】:2019-09-10 07:39:24
【问题描述】:

我有一个 Pandas DataFrame 对象 data 与列 'a', 'b', 'c', ..., 'z'

我想选择满足以下条件的所有行:'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

【问题讨论】:

  • 你能把你得到的错误贴出来吗?
  • @toti08 添加了问题中的错误

标签: python pandas dataframe na


【解决方案1】:

我想选择所有符合以下条件的行:'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

【讨论】:

    猜你喜欢
    • 2020-10-06
    • 1970-01-01
    • 2015-05-03
    • 1970-01-01
    • 2022-07-08
    • 1970-01-01
    • 2022-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多