0

My file contains a column called 'Id';

eg:

Id  bill
---------
1   aaa
2   bbb
3   ccc
1   ddd
2   ee

I would like to return the count of Ids. Here it should be count(data['Id')) = 3 (not 5)

print(df.groupby('Id').count()) prints the whole file with rows count and column count. How can I make sure simple it prints how many unique Ids present in the column?

2
  • 1
    df["Id"].nunique(). Commented Oct 10, 2019 at 4:25
  • @HenryYik thanks this works Commented Oct 10, 2019 at 4:37

3 Answers 3

2

Use Series.nunique:

a = df["Id"].nunique()
print (a)
3

Or convert values to sets and get length:

a = len(set(df["Id"]))
print (a)
3
Sign up to request clarification or add additional context in comments.

Comments

1

In your original attempt just use the length of the returned dataframe using len()

print(len(df.groupby('Id').count()))

Comments

1

You can also use collections.Counter, assume tmp is your dataframe

from collections import Counter

count = Counter(tmp['Id'])

Comments

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.