Isolating DataFrame instances where column A = one specific code, but column B = multiple different descriptions

Viewed 42

I have a dataframe where one column is the account code and the secondary column is the description, and need to flag up instances where an account code has more than one description present. For example to take this:

  Acc. Code   Description
0      0001 Description 1
1      0002 Description 2
2      0002 Description 2
3      0003 Description 3 
4      0003       Desc. 3 
5      0004 Description 4
6      0005 Description 5
7      0006 Description 6
8      0006       Desc. 6 
9      0006     desc. Six 
10     0007 Description 1

and adjust it so I have a dataframe like this, which will later be used to choose which description to prioritise and map for all instances of that code:

  Acc. Code   Description
0      0003 Description 3 
1      0003       Desc. 3 
2      0006 Description 6
3      0006       Desc. 6 
4      0006     desc. Six 

I haven't been able to find anything specific to pandas to filter for duplicates based on multiple column values.

2 Answers

Yuo can use value counts to filter your dataframe if those are the only columns

df[df['Code'].isin(df['Code'].value_counts().loc[lambda x: x>1].index)]

You can use .groupby() + .filter() where you you filter according .nunique() size:

x = df.groupby("Acc. Code").filter(lambda x: x["Description"].nunique() > 1)
print(x)

Prints:

   Acc. Code    Description
2          3  Description 3
3          3        Desc. 3
6          6  Description 6
7          6        Desc. 6
8          6      desc. Six
Related