我有一個定義如下的資料框。當input列從 1 變為 0 但沒有零時,我想計算天數(或行數):
import pandas as pd
df = pd.DataFrame({'input': [1,1,1,0,0,0,1,1,1,0,0,0]},
index=pd.date_range('2021-10-01', periods=12))
# I can mark the points of interest, i.e. when it goes from 1 to 0
df['change'] = 0
df.loc[(df['input'].shift(1) - df['input']) > 0, 'change'] = 1
print(df)
我最終得到以下結果:
input change
2021-10-01 1 0
2021-10-02 1 0
2021-10-03 1 0
2021-10-04 0 1
2021-10-05 0 0
2021-10-06 0 0
2021-10-07 1 0
2021-10-08 1 0
2021-10-09 1 0
2021-10-10 0 1
2021-10-11 0 0
2021-10-12 0 0
我想要的是一個res輸出,所以每次我得到一個重新開始計數:
input change res
2021-10-01 1 0 0
2021-10-02 1 0 0
2021-10-03 1 0 0
2021-10-04 0 1 1
2021-10-05 0 0 2
2021-10-06 0 0 3
2021-10-07 1 0 4
2021-10-08 1 0 5
2021-10-09 1 0 6
2021-10-10 0 1 1
2021-10-11 0 0 2
2021-10-12 0 0 3
請注意,它與問題How to count the number of days since a column flag非常相似?但在案例之間沒有零。
uj5u.com熱心網友回復:
您可以使用groupby生成每 1 次重新啟動的組,然后cumcount:
s = df['change'].cumsum()
df['res'] = s.groupby(s).cumcount().add(1).mask(s.eq(0), 0)
輸出:
input change res
2021-10-01 1 0 0
2021-10-02 1 0 0
2021-10-03 1 0 0
2021-10-04 0 1 1
2021-10-05 0 0 2
2021-10-06 0 0 3
2021-10-07 1 0 4
2021-10-08 1 0 5
2021-10-09 1 0 6
2021-10-10 0 1 1
2021-10-11 0 0 2
2021-10-12 0 0 3
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/359138.html
上一篇:這個二分搜索是如何作業的?
