gpt4 book ai didi

python - Dask:在列上删除 NA?

转载 作者:太空狗 更新时间:2023-10-30 02:52:34 25 4
gpt4 key购买 nike

我尝试应用过滤器来删除 DASK 数据帧中包含太多 NA 的列:

df.dropna(axis=1, how='all', thresh=round(len(df) * .8))

不幸的是,dask dropna API 与 pandas 的 API 略有不同,它既不接受 axis 也不接受 threshold。绕过它的一种部分方法是逐列迭代并删除那些常量(不管它们是否填充了 NA,因为我不介意去掉常量):

    for col in df.columns:
if len(df[col].unique()) == 1:
new_df = df.drop(col, axis = 1)

但这并不能让我应用阈值。我可以通过添加手动计算阈值:

elif sum(df[col].isnull().compute()) / len(df[col]) > 0.8:
new_df = df.drop(col, axis = 1)

但我不确定此时调用 computelen 是否最佳,我很想知道是否有更好的方法来解决这个问题?

最佳答案

2021 年 8 月 10 日更新:

现在 Dask 有 axis , thresh , 和 subset可能有帮助的参数。之前的答案可以重写为:

df.dropna(subset=columns_to_inspect, thresh=threshold_to_drop_na, axis=1)

旧答案

你是对的,没有办法通过使用 df.dropna() 来做到这一点.

我建议使用这个等式 df.loc[:,df.isnull().sum()<THRESHOLD]

关于python - Dask:在列上删除 NA?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52850269/

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