1 回答

TA貢獻1836條經驗 獲得超5個贊
如果我正確理解了這個問題,您可以執行以下操作:創建掩碼s以將每個連續值分成單獨的組。從,為每個組的每個值s創建掩碼。s1Groupby s1and Dateand doing cumcountandmap創建所需的輸出:
s = df.Date.ne(df.Date.shift()).cumsum()
s1 = df.Date.groupby(s).cumcount()
df['Month'] = df.groupby([s1, 'Date']).Name.cumcount().add(1).map(lambda x: 'Month '+str(x))
Out[897]:
Name Task Team Date Month
0 John Market-study A 1 Month 1
1 Michael Customer-service B 1 Month 1
2 Joanna Accounting C 1 Month 1
3 John Accounting B 2 Month 1
4 Michael Customer-service A 2 Month 1
5 Joanna Market-study C 2 Month 1
6 John Customer-service C 1 Month 2
7 Michael Market-study A 1 Month 2
8 Joanna Customer-service B 1 Month 2
9 John Market-study A 2 Month 2
10 Michael Customer-service B 2 Month 2
11 Joanna Accounting C 2 Month 2
添加回答
舉報