gpt4 book ai didi

python - 评估所有列后删除 pandas 数据框中的行

转载 作者:太空宇宙 更新时间:2023-11-03 18:01:37 24 4
gpt4 key购买 nike

我有一个非常大的 pandas DataFrame(>1 亿行,>1000 列)。每行都有一个唯一的标签作为索引,对于大多数行,只有一列包含值。我想通过删除只有一列具有值的那些行并保留具有两列以上具有值的那些行来创建一个新的 DataFrame。

最佳答案

您可以使用 dropna 删除它们:

In [3]:
#sample df
df = pd.DataFrame({'a':[0,NaN, 2,3,4], 'b':[0,NaN, 2,3,NaN], 'c':arange(5)})
df

Out[3]:
a b c
0 0 0 0
1 NaN NaN 1
2 2 2 2
3 3 3 3
4 4 NaN 4
In [5]:
# drop just the rows which have 2 or more NaN values
df.dropna(thresh=2, axis=0)
Out[5]:
a b c
0 0 0 0
2 2 2 2
3 3 3 3
4 4 NaN 4

您传递参数 thresh=2 来指定您至少需要 2 个非 NA 值,并且 axis=0 将指定应应用该条件的行-明智的。

关于python - 评估所有列后删除 pandas 数据框中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27593958/

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