[英]count the frequency that a value occurs in the end of a dataframe column
我有一個數據集
|category|
cat a
cat a
cat b
cat b
cat a
cat b
cat a
cat a
cat a
cat a
我希望能夠返回在 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.