gpt4 book ai didi

python - 删除重复项,但忽略空值

转载 作者:行者123 更新时间:2023-12-02 02:16:05 25 4
gpt4 key购买 nike

所以我知道你可以使用这样的东西来删除重复的行:

the_data.drop_duplicates(subset=['the_key'])

但是,如果某些值的 the_key 为 null,如下所示:

   the_key  C  D
1 NaN * *
2 NaN *
3 111 * *
4 111

它将保留在 C 列中标记的内容。是否可以让 drop_duplicates 将所有 nan 视为不同的,并获得像 D 列中那样保留数据的输出?

最佳答案

使用duplicated链接为 isna并按boolean indexing过滤:

df = df[(~df['the_key'].duplicated()) | df['the_key'].isna()]
#fol oldier pandas versions
#df = df[(~df['the_key'].duplicated()) | df['the_key'].isnull()]
print (df)
the_key C D
1 NaN * *
2 NaN *
3 111.0 * *

关于python - 删除重复项,但忽略空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50154835/

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