簡體   English   中英

計算一個值出現在 dataframe 列末尾的頻率

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM