0

I have a df as the following:

col1  col2
-----------
a      1b
a      1b
a      1a
b      2a
b      3f

And I want to count how many unique pairs each col1 element has: output:

(a, 2)
(b, 2)
1
  • 1
    groupby & nunique Commented Feb 3, 2022 at 14:29

4 Answers 4

1

You want to count the number if unique values on col2 after grouping on col1 -

df.groupby(['col1']).nunique()['col2']
#col1
#a    2
#b    2

If you want it in the format you mentioned, you can pass it into zip -

list(zip(df.groupby(['col1']).nunique()['col2'].index, df.groupby(['col1']).nunique()['col2'].values))                           
[('a', 2), ('b', 2)]
Sign up to request clarification or add additional context in comments.

Comments

1

As a DataFrame

df.groupby("col1", as_index=False).nunique()

    col1 col2
0    a    2
1    b    2

In the format mentioned;

list(df.groupby("col1", as_index=False).nunique().to_records(index=False))

[('a', 2), ('b', 2)]

Comments

0
df.drop_duplicates(['col1', 'col2'])[['col1']].value_counts()

or

list(map(tuple, df.groupby('col1', as_index=False).nunique().values))

Comments

-1
df.groupby(['col1', 'col2']).size()

2 Comments

That doesn't give the expected result
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.