我有一個看起來像我可以按列分組並重新取樣日期嗎?
CustomerID InvoiceDate
13654.0 2011-07-17 13:29:00
14841.0 2010-12-16 10:28:00
19543.0 2011-10-18 16:58:00
12877.0 2011-06-15 13:34:00
15073.0 2011-06-06 12:33:00
我感興趣的是,在該客戶購買的速度有些消費者購買數據。我想按每個客戶進行分組,然後確定每季度進行了多少次採購(假設每個季度是從一月份開始的每3個月)。
我可以定義每個季度的開始和結束時間,並製作另一列。我想知道是否可以用groupby
來達到同樣的效果。
目前,這是我要做的事:
r = data.groupby('CustomerID')
frames = []
for name,frame in r:
f =frame.set_index('InvoiceDate').resample("QS").count()
f['CustomerID']= name
frames.append(f)
g = pd.concat(frames)
關閉。我也想按客戶ID進行分組。我找到了一個辦法,謝謝。 –
@DemetriP,我已經更新了答案 - 請檢查 – MaxU
好得多。謝謝! –