Combining groupby() and resample()
A very powerful method in Pandas is .groupby()
. Whereas .resample()
groups rows by some time or date information, .groupby()
groups rows based on the values in one or more columns. For example, rides.groupby('Member type').size()
would tell us how many rides there were by member type in our entire DataFrame.
.resample()
can be called after .groupby()
. For example, how long was the median ride by month, and by Membership type?
This is a part of the course
“Working with Dates and Times in Python”
Exercise instructions
- Complete the
.groupby()
call to group by'Member type'
, and the.resample()
call to resample according to'Start date'
, by month. - Print the median
Duration
for each group.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Group rides by member type, and resample to the month
grouped = rides.groupby('____')\
.resample(____, ____)
# Print the median duration for each group
print(grouped[____].____)
This exercise is part of the course
Working with Dates and Times in Python
Learn how to work with dates and times in Python.
To conclude this course, you'll apply everything you've learned about working with dates and times in standard Python to working with dates and times in Pandas. With additional information about each bike ride, such as what station it started and stopped at and whether or not the rider had a yearly membership, you'll be able to dig much more deeply into the bike trip data. In this chapter, you'll cover powerful Pandas operations, such as grouping and plotting results by time.
Exercise 1: Reading date and time data in PandasExercise 2: Loading a csv file in PandasExercise 3: Making timedelta columnsExercise 4: Summarizing datetime data in PandasExercise 5: How many joyrides?Exercise 6: It's getting cold outside, W20529Exercise 7: Members vs casual riders over timeExercise 8: Combining groupby() and resample()Exercise 9: Additional datetime methods in PandasExercise 10: Timezones in PandasExercise 11: How long per weekday?Exercise 12: How long between rides?Exercise 13: Wrap-upWhat is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.