gpt4 book ai didi

python - 删除超过 70% 零的列

转载 作者:太空宇宙 更新时间:2023-11-04 09:59:43 25 4
gpt4 key购买 nike

我想知道是否有一个命令可以删除具有超过 70% 的零或 X% 的零的列。喜欢:

     df = df.loc[:, df.isnull().mean() < .7]

对于 NaN。

谢谢!

最佳答案

只需将 df.isnull().mean() 更改为 (df==0).mean():

df = df.loc[:, (df==0).mean() < .7]

这是一个演示:

df
Out:
0 1 2 3 4
0 1 1 1 1 0
1 1 0 0 0 1
2 0 1 1 0 0
3 1 0 0 1 0
4 1 1 1 1 1
5 1 0 0 0 0
6 0 1 0 0 0
7 0 1 1 0 0
8 1 0 0 1 0
9 0 0 0 1 0

(df==0).mean()
Out:
0 0.4
1 0.5
2 0.6
3 0.5
4 0.8
dtype: float64

df.loc[:, (df==0).mean() < .7]
Out:
0 1 2 3
0 1 1 1 1
1 1 0 0 0
2 0 1 1 0
3 1 0 0 1
4 1 1 1 1
5 1 0 0 0
6 0 1 0 0
7 0 1 1 0
8 1 0 0 1
9 0 0 0 1

关于python - 删除超过 70% 零的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44250642/

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