3

I have this dataframe:

     content
id         
17         B
17         A
 6         A
15         A
...

I want to count how many rows have the index 17 (in this case that would be 2). Is there a way to do that?

0

3 Answers 3

3

You can try:

sum(df.index == 17)

df.index == 17 returns an array with boolean with True when index value matches else False. And while using sum function True is equivalent to 1.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm getting results like [1 1 1 ..., 0 0 0]. I just want to know how many times 17 appears in index
@abutremutante do you mean while using df.index == 17, you got [1 1 1 ..., 0 0 0]? Doesn't it work for the sample example provided?
you're right, it does. since my df is huge I tried to simplify here but apparently wasn't the best approach. anyway, I solved in a not very pythonic way: a=0, for i in df.index: if i == 17: a+=1
3

You can groupby level

df.groupby(level=0).count()

Or reset_index()

df.reset_index().groupby('id').count()

3 Comments

but how do I access the 17? df.groupby(level=0).count().loc[17]?
@abutremutante df[df.index==17]
Hi, my case has another column which is date. How could I count row on condition that date >= '2019-01-01' (for example)
3

Problem: How to count the quantity of index label?

Input: # Your DataFrame
       test_dict = {'id': ['17', '17', '6', '15'], 'content': ['B', 'A', 'A', 'A']}
       testd_df = pd.DataFrame.from_dict(test_dict) # create DataFrame from dict
       testd_df.set_index('id', inplace=True) # set 'id' as index in inplace way
       testd_df
Output: 
             |content
        --------------
         id  |
        -------------
         17  |      B
         17  |      A
          6  |      A
         15  |      A

Solution: Use api pandas.Index.value_counts

Based on the document, pandas.Index.value_counts will return object containing counts of unique values and return a pd.Series.

so now, I can select the specific index I want by using pandas.Series.loc (not get confused with .iloc)

# Solution
Input:  index_count = pd.Index(testd_df.index).value_counts() # count value of unique value
        index_count

Output: 17    2
        15    1
        6     1
        dtype: int64
---------------------------------
Input:  index_count.loc['17'] # select the information you care about
Output: 2

1 Comment

There's no need to create a new, index, just use testd_df.index.value_counts()

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.