0

I am trying to find the number of unique values that cover 2 fields. So for example, a typical example would be last name and first name. I have a data frame.

enter image description here

When I do the following, I just get the number of unique fields for each column, in this case, Last and First. Not a composite.

df[['Last Name','First Name']].nunique()

Thanks!

Rodalm
  • 5,169
  • 5
  • 21
Kulwant
  • 641
  • 2
  • 11
  • 28
  • See https://stackoverflow.com/questions/35268817/unique-combinations-of-values-in-selected-columns-in-pandas-data-frame-and-count – topsail Jun 15 '22 at 22:04

2 Answers2

3

Groupby both columns first, and then use nunique

>>> df.groupby(['First Name', 'Last Name']).nunique()
rafaelc
  • 57,686
  • 15
  • 58
  • 82
0

IIUC, you could use value_counts() for that:

df[['Last Name','First Name']].value_counts().size

3

For another example, if you start with this extended data frame that contains some dups:

  Last Name First Name
0     Smith       Bill
1   Johnson       Bill
2     Smith       John
3    Curtis       Tony
4    Taylor  Elizabeth
5     Smith       Bill
6   Johnson       Bill
7     Smith       Bill

Then value_counts() gives you the counts by unique composite last-first name:

df[['Last Name','First Name']].value_counts()

Last Name  First Name
Smith      Bill          3
Johnson    Bill          2
Curtis     Tony          1
Smith      John          1
Taylor     Elizabeth     1

Then the length of that frame will give you the number of unique composite last-first names:

df[['Last Name','First Name']].value_counts().size

5
jch
  • 3,600
  • 1
  • 15
  • 17