Python / Pandas - 统计特定索引的行数

3

I have this dataframe:

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

我想要统计索引为17的行有多少条(在这个例子中是2)。 有没有一种方法可以做到这一点?

3个回答

3
你可以尝试以下方法:
sum(df.index == 17)

df.index == 17 返回一个数组,其中boolean值为True表示索引值匹配,否则为False。同时,在使用sum函数时,True等同于1


我得到了像[1 1 1 ..., 0 0 0]这样的结果。我只想知道索引中17出现了多少次。 - aabujamra
@abutremutante 你是指在使用 df.index == 17 的时候,你得到了 [1 1 1 ..., 0 0 0] 吗?这对于提供的示例样本不起作用吗? - niraj
1
你说得对,确实是这样。因为我的数据框很大,我试图在这里简化它,但显然不是最好的方法。无论如何,我用了一种不太符合Python风格的方式解决了:a = 0,对于df.index中的每个i:如果i == 17,则a + = 1 - aabujamra

3

问题: 如何计算索引标签的数量?

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

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

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

因此,现在我可以使用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

不需要创建新的索引,只需使用 testd_df.index.value_counts() - sanzoghenzo

2
你可以按级别进行分组。
df.groupby(level=0).count()

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

但是我如何访问17呢?df.groupby(level=0).count().loc[17]? - aabujamra
1
返回索引值为17的行:df[df.index==17] - BENY
嗨,我的情况有另一列是日期。如果要根据日期 >= '2019-01-01'(例如)计算行数,应该怎么做呢? - Chau Loi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接