gpt4 book ai didi

python - 从 pandas 数据框中选择行,其中指定列不全为 NaN

转载 作者:太空宇宙 更新时间:2023-11-03 17:09:00 26 4
gpt4 key购买 nike

我有一个 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

最佳答案

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

关于python - 从 pandas 数据框中选择行,其中指定列不全为 NaN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34311957/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com