Python / Pandas - 计算具有特定索引的行数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45851282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:18:39  来源:igfitidea点击:

Python / Pandas - Count number of rows with certain index

pythonpandas

提问by abutremutante

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?

我想计算有多少行索引为 17(在本例中为 2)。有没有办法做到这一点?

回答by student

You can try:

你可以试试:

sum(df.index == 17)

df.index == 17returns an array with booleanwith Truewhen index value matches else False. And while using sumfunction Trueis equivalent to 1.

df.index == 17返回以与阵列booleanTrue当索引值其他匹配False。而使用sum函数True相当于1.

回答by WY Hsu

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

解决方案:使用api pandas.Index.value_counts

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

根据文档,pandas.Index.value_counts将返回包含唯一值计数的对象并返回一个pd.Series.

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

所以现在,我可以选择我想要的特定索引pandas.Series.loc(不要混淆.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

回答by YOBEN_S

You can groupby level

您可以按级别分组

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

Or reset_index()

或者 reset_index()

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