简体   繁体   English

计算一个值出现在 dataframe 列末尾的频率

[英]count the frequency that a value occurs in the end of a dataframe column

I have a dataset我有一个数据集

|category|
cat a
cat a
cat b
cat b
cat a
cat b
cat a
cat a
cat a
cat a

I'd like to be able to return the total 4 for cat a that occurs at the end of the dataframe in other words if cat b happens then the count starts over once another cat a is found我希望能够返回在 dataframe 末尾发生的cat a的总数4 ,换句话说,如果cat b发生,那么一旦找到另一cat a ,计数就会重新开始

df['category'] = np.where(df['category'] == "cat b", df['category'],np.nan)
df['category'].bfill().isna().sum()
>>>4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM