結合 groupby() 與 resample()
Pandas 中非常強大的方法是 .groupby()。.resample() 會依照時間或日期資訊分組列,而 .groupby() 會依照一或多個欄位的值來分組列。例如,rides.groupby('Member type').size() 會告訴我們整個 DataFrame 中各會員類型的騎乘次數。
你可以在 .groupby() 之後呼叫 .resample()。例如:按月份與會員類型來看,中位數騎乘時間有多長?
本練習屬於課程
在 Python 處理日期與時間
練習說明
- 完成
.groupby()呼叫,依'Member type'分組;並完成.resample()呼叫,依'Start date'以每月進行重採樣。 - 印出每個分組的
Duration中位數。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Group rides by member type, and resample to the month
grouped = rides.groupby('____')\
.resample('____', on = '____')
# Print the median duration for each group
print(grouped[____].____)