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?
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.
df.index == 17, you got [1 1 1 ..., 0 0 0]? Doesn't it work for the sample example provided?You can groupby level
df.groupby(level=0).count()
Or reset_index()
df.reset_index().groupby('id').count()
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
pandas.Index.value_countsBased 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
testd_df.index.value_counts()