|  | 
|  | 1 | +import pandas as pd | 
|  | 2 | +import matplotlib.pyplot as plt | 
|  | 3 | +import numpy as np | 
|  | 4 | + | 
|  | 5 | +# Import bird data | 
|  | 6 | +birddata = pd.read_csv('https://d37djvu3ytnwxt.cloudfront.net/assets/' | 
|  | 7 | +'courseware/v1/c72498a54a4513c2eb4ec005adc0010c/' | 
|  | 8 | +'asset-v1:HarvardX+PH526x+3T2016+type@asset+block/bird_tracking.csv') | 
|  | 9 | + | 
| 1 | 10 | # First, use `groupby` to group up the data. | 
| 2 | 11 | grouped_birds = birddata.groupby("bird_name") | 
| 3 | 12 | 
 | 
| 4 | 13 | # Now operations are performed on each group. | 
| 5 | 14 | mean_speeds = grouped_birds.speed_2d.mean() | 
| 6 | 15 | 
 | 
| 7 | 16 | # The `head` method prints the first 5 lines of each bird. | 
| 8 |  | -grouped_birds.head() | 
|  | 17 | +# This is useful if you are running this in ipython and need to see some data. | 
|  | 18 | +# Call this only on pandas data frame. | 
|  | 19 | +# mean_speeds.head() | 
| 9 | 20 | 
 | 
| 10 | 21 | # Find the mean `altitude` for each bird. | 
| 11 | 22 | # Assign this to `mean_altitudes`. | 
| 12 | 23 | 
 | 
| 13 | 24 | mean_altitudes = grouped_birds.altitude.mean() | 
| 14 | 25 | 
 | 
| 15 | 26 | 
 | 
| 16 |  | - | 
| 17 |  | - | 
| 18 | 27 | # Convert birddata.date_time to the `pd.datetime` format. | 
| 19 | 28 | birddata.date_time = pd.to_datetime(birddata.date_time) | 
| 20 | 29 | 
 | 
| 21 | 30 | # Create a new column of day of observation | 
| 22 | 31 | birddata["date"] = birddata.date_time.dt.date | 
| 23 | 32 | 
 | 
| 24 |  | -# Check the head of the column. | 
| 25 |  | -birddata.date.head() | 
|  | 33 | +# If you're in ipython and want to check the head of the column run: | 
|  | 34 | +# birddata.date.head() | 
| 26 | 35 | 
 | 
| 27 | 36 | grouped_bydates = birddata.groupby("date") | 
| 28 | 37 | mean_altitudes_perday = grouped_bydates.altitude.mean() | 
|  | 
| 33 | 42 | grouped_birdday = birddata.groupby(["bird_name","date"]) | 
| 34 | 43 | mean_altitudes_perday = grouped_birdday.altitude.mean() | 
| 35 | 44 | 
 | 
| 36 |  | -# look at the head of `mean_altitudes_perday`. | 
| 37 |  | -mean_altitudes_perday.head() | 
|  | 45 | +# If you're in ipython and want to look at the head of `mean_altitudes_perday` run: | 
|  | 46 | +# mean_altitudes_perday.head() | 
| 38 | 47 | 
 | 
| 39 | 48 | 
 | 
| 40 | 49 | grouped_birdday = birddata.groupby(["bird_name","date"]) | 
|  | 
| 48 | 57 | sanne_daily_speed.plot(label="Sanne") | 
| 49 | 58 | nico_daily_speed.plot(label="Nico") | 
| 50 | 59 | plt.legend(loc="upper left") | 
|  | 60 | +plt.ylabel("2D mean speed (m/s)") | 
| 51 | 61 | plt.show() | 
0 commit comments