-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhomework7_case_study_5_bird_migration.py
More file actions
67 lines (54 loc) · 1.87 KB
/
homework7_case_study_5_bird_migration.py
File metadata and controls
67 lines (54 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#------------------------------------------------
# Homework 7 of course "Python for Research"
# HarvardX - PH526xEdX, EdX
# Bird migration
#------------------------------------------------
import pandas as pd
birddata=pd.read_csv("bird_tracking.csv")
import matplotlib.pyplot as plt
import numpy as np
bird_names=pd.unique(birddata.bird_name)
#Exercise 1--------------------
#Group data frame by bird name
grouped_birds=birddata.groupby(by='bird_name')
grouped_birds[ix].head()
#Mean speed of each bird
grouped_birds.speed_2d.mean()
mean_altitude=grouped_birds.altitude.mean()
bird_names=pd.unique(birddata.bird_name)
bird_names[0]
grouped_birds
ix=grouped_birds.bird_name=='Eric'
ix
for name in bird_names:
ix=birddata.bird_name==name
birddata[ix].head()
#much shorter:
grouped_birds.head()
#Excercise 2--------------------
# Convert birddata.date_time to the `pd.datetime` format.
birddata.date_time = pd.to_datetime(birddata.date_time)
#Extract only date
onlydate=birddata.date_time.dt.date
birddata["date"]=onlydate
#Use `groupby()` to group the data by date.
grouped_bydates = birddata.groupby(by="date")
type(grouped_bydates)
# Find the mean `altitude` for each date.
mean_altitudes_perday=grouped_bydates.altitude.mean()
#Excercise 3--------------------
#group by two columns, first bird name, then date
# Example: df.groupby(['name', 'title'])
grouped_birdday = birddata.groupby(by=['bird_name','date'])
mean_altitudes_perday=grouped_birdday.altitude.mean()
#Excercise 4--------------------
#Panda series object are accessed as normal arrays
mean_speed_perday=grouped_birdday.speed_2d.mean()
eric_daily_speed = mean_speed_perday['Eric']
sanne_daily_speed = mean_speed_perday['Sanne']
nico_daily_speed = mean_speed_perday['Nico']
eric_daily_speed.plot(label="Eric")
sanne_daily_speed.plot(label="Sanne")
nico_daily_speed.plot(label="Nico")
plt.legend(loc="upper left")
plt.show()