从Pandas DataFrame中删除重复项,并保留原始条件 [英] Removing duplicates from Pandas dataFrame with condition for retaining original
本文介绍了从Pandas DataFrame中删除重复项,并保留原始条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我具有以下DataFrame:
Assuming I have the following DataFrame:
A | B
1 | Ms
1 | PhD
2 | Ms
2 | Bs
我想删除与A列有关的重复行,并且我想保留B列中值为"PhD"的行作为原始行,如果找不到"PhD",我想保留B列中带有"Bs"的行.
I want to remove the duplicate rows with respect to column A, and I want to retain the row with value 'PhD' in column B as the original, if I don't find a 'PhD', I want to retain the row with 'Bs' in column B.
我正在尝试使用
df.drop_duplicates('A')
有条件
推荐答案
>>> df
A B
0 1 Ms
1 1 Ms
2 1 Ms
3 1 Ms
4 1 PhD
5 2 Ms
6 2 Ms
7 2 Bs
8 2 PhD
使用自定义功能对数据框进行排序:
Sorting a dataframe with a custom function:
def sort_df(df, column_idx, key):
'''Takes a dataframe, a column index and a custom function for sorting,
returns a dataframe sorted by that column using that function'''
col = df.ix[:,column_idx]
df = df.ix[[i[1] for i in sorted(zip(col,range(len(col))), key=key)]]
return df
我们的排序功能:
cmp = lambda x:2 if 'PhD' in x else 1 if 'Bs' in x else 0
实际情况:
sort_df(df,'B',cmp).drop_duplicates('A', take_last=True)
A B
4 1 PhD
8 2 PhD
这篇关于从Pandas DataFrame中删除重复项,并保留原始条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文