简体   繁体   English

如何在不删除 NaN 值的情况下删除 pandas 中的重复项

[英]How can I drop duplicates in pandas without dropping NaN values

I have a dataframe which I query and I want to get only unique values out of a certain column.我有一个 dataframe 查询,我只想从某个列中获取唯一值。
I tried to do that executing this code:我尝试执行此代码:

    database = pd.read_csv(db_file, sep='\t')
    query = database.loc[database[db_specifications[0]].isin(elements)].drop_duplicates(subset=db_specification[1])

db_specification is just a list containing two columns that I query. db_specification只是一个包含我查询的两列的列表。
Some of the values are NaN and I don't want to consider them duplicates of each other, how can I achieve that?有些值是NaN ,我不想认为它们是重复的,我该如何实现呢?

You can start by selecting all NaN and then drop duplicate on the rest of the dataframe.您可以首先选择所有NaN ,然后在 dataframe 的 rest 上删除重复项。

mask = data.isna().any()
data = pd.concat([data[mask], data[~mask]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM