starguardian23
starguardian23

Reputation: 3

pandas count unique values considering column

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)

Upvotes: 0

Views: 67

Answers (4)

Elton da Mata
Elton da Mata

Reputation: 308

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

or

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

Upvotes: 0

Jyr
Jyr

Reputation: 711

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)]

Upvotes: 1

Mortz
Mortz

Reputation: 4879

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)]

Upvotes: 1

N27
N27

Reputation: 31

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

Upvotes: -1

Related Questions