gpt4 book ai didi

python - 如果列具有一定百分比的 NaN 值,则返回列 (Python)

转载 作者:行者123 更新时间:2023-12-01 06:47:30 24 4
gpt4 key购买 nike

希望仅返回至少有 25% NaN 值的列作为新的 df

I'm thinking either a conditional statement using .loc, .isnull, or count, but I'm not certain what the most efficient method is. Appreciate any and all assistance.

DF:


df1:
(axis 1 = A,B,C = series)

A B C
1 1 2 1
2 NaN NaN 3
3 4 NaN 1
4 2 NaN 4

思考:

df.loc[df['series'] == nan >= 25% ]

或者类似的东西:


if count(nan) for column(x) in 'series' is >= (.25 * (count(x)))
return loc[x]

返回新数据框:

df2:

A B
1 1 2
2 NaN NaN
3 4 NaN
4 2 NaN

返回 A 和 B,因为它们中至少有 25% 的列条目为 NaN(缺失)

最佳答案

基于 https://datascience.stackexchange.com/q/12645 的回复.

na_count_mask = df.isna().sum(axis=0) >= (col_count // 4)

res_df = df.loc[na_count_mask]

关于python - 如果列具有一定百分比的 NaN 值,则返回列 (Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59161809/

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