<jupyter_start><jupyter_text>Using Python for Research Homework: Week 4, Case Study 2In this case study, we will continue taking a look at patterns of flight for each of the three birds in our dataset.<jupyter_code># DO NOT EDIT THIS CODE
import pandas as pd
import numpy as np
birddata = pd.read_csv("https://courses.edx.org/asset-v1:HarvardX+PH526x+2T2019+type@asset+block@bird_tracking.csv", index_col=0)
birddata.head()<jupyter_output><empty_output><jupyter_text>Exercise 1In this case study, we will continue taking a look at patterns of flight for each of the three birds in our dataset. We will group the flight patterns by bird and date, and plot the mean altitude for these groupings.`pandas` makes it easy to perform basic operations on groups within a dataframe without needing to loop through each value in the dataframe. In this exercise, we will group the dataframe by `birdname` and then find the average `speed_2d` for each bird. Instructions - Fill in the code to find the mean altitudes of each bird using the pre-loaded `birddata` dataframe.<jupyter_code># First, use `groupby()` to group the data by "bird_name".
grouped_birds = birddata.groupby("bird_name")
print(grouped_birds)
# Now calculate the mean of `speed_2d` using the `mean()` function.
mean_speeds = birddata.speed_2d.mean()
print(mean_speeds)
# Find the mean `altitude` for each bird.
mean_altitudes = grouped_birds.altitude.mean()
print(mean_altitudes)<jupyter_output><pandas.core.groupby.generic.DataFrameGroupBy object at 0x000002B656999828>
2.558986595840664
bird_name
Eric     60.249406
Nico     67.900478
Sanne    29.159922
Name: altitude, dtype: float64<jupyter_text>Exercise 2In this exercise, we will group the flight times by date and calculate the mean altitude within that day. Instructions - Convert `birddata.date_time` to the `pd.datetime` format, and store as `birddata["date"]`.- Fill in the code to find the mean altitudes for each day.<jupyter_code># Convert birddata.date_time to the `pd.datetime` format.
birddata.date_time = pd.to_datetime(birddata.date_time)

# Create a new column of day of observation
birddata["date"] = birddata.date_time.dt.date

# Use `groupby()` to group the data by date.
grouped_bydates = birddata.groupby('date')

# Find the mean `altitude` for each date.
mean_altitudes_perday =  grouped_bydates.altitude.mean()
print(mean_altitudes_perday)<jupyter_output>date
2013-08-15    134.092000
2013-08-16    134.839506
2013-08-17    147.439024
2013-08-18    129.608163
2013-08-19    180.174797
2013-08-20    184.461224
2013-08-21    171.546185
2013-08-22    114.172691
2013-08-23    144.716049
2013-08-24    101.446721
2013-08-25     93.162602
2013-08-26     75.995951
2013-08-27     84.591093
2013-08-28    111.210526
2013-08-29    177.421488
2013-08-30     92.520161
2013-08-31    160.975904
2013-09-01     84.753086
2013-09-02     88.128514
2013-09-03     93.975709
2013-09-04     90.717213
2013-09-05     87.264000
2013-09-06     89.020492
2013-09-07    103.412955
2013-09-08     76.566265
2013-09-09     81.483471
2013-09-10     90.618852
2013-09-11     77.222672
2013-09-12     75.646091
2013-09-13     66.615079
                 ...    
2014-04-01    115.797571
2014-04-02     13.589431
2014-04-03     26.772727
2014-04-04    101.441667
2014-04-05     96.995536
2014-04-06     13.426540
2014-04-07     27.105000
2014-04-08      9.597087
2014-04-09     22.84[...]<jupyter_text>Exercise 3In this exercise, we will group the flight times by both bird and date, and calculate the mean altitude for each. Instructions - `birddata` already contains the `date` column. To find the average speed for each bird and day, create a new grouped dataframe called `grouped_birdday` that groups the data by both `bird_name` and date.<jupyter_code># Use `groupby()` to group the data by bird and date.
grouped_birdday = birddata.groupby(['bird_name','date'])

# Find the mean `altitude` for each bird and date.
mean_altitudes_perday = grouped_birdday.altitude.mean()
print(mean_altitudes_perday)<jupyter_output>bird_name  date      
Eric       2013-08-15     74.988095
           2013-08-16    127.773810
           2013-08-17    125.890244
           2013-08-18    121.353659
           2013-08-19    134.928571
           2013-08-20    101.792683
           2013-08-21    160.518072
           2013-08-22     99.674699
           2013-08-23    122.710843
           2013-08-24    119.674699
           2013-08-25    108.500000
           2013-08-26     77.107143
           2013-08-27    108.809524
           2013-08-28    134.464286
           2013-08-29    138.172840
           2013-08-30    131.226190
           2013-08-31     63.166667
           2013-09-01    106.765432
           2013-09-02    106.975904
           2013-09-03    145.353659
           2013-09-04    129.253012
           2013-09-05    112.380952
           2013-09-06    123.952381
           2013-09-07    120.325301
           2013-09-08    112.702381
           2013-09-09    144.060241
           2013-09-10    121.785714
      [...]<jupyter_text>Exercise 4Great! Now find the average speed for each bird and day. Instructions - Store these are three `pandas` `Series` objects, one for each bird.- Use the plotting code provided to plot the average speeds for each bird.<jupyter_code>import matplotlib.pyplot as plt

eric_daily_speed  = grouped_birdday.speed_2d.mean()['Eric']
sanne_daily_speed = grouped_birdday.speed_2d.mean()['Sanne']
nico_daily_speed  = grouped_birdday.speed_2d.mean()['Nico']
print(nico_daily_speed)

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()<jupyter_output>date
2013-08-15     2.856924
2013-08-16     3.386387
2013-08-17     3.123210
2013-08-18     2.785776
2013-08-19     2.920577
2013-08-20     2.730114
2013-08-21     2.759074
2013-08-22     2.020524
2013-08-23     3.152283
2013-08-24     1.840692
2013-08-25     2.154902
2013-08-26     2.506818
2013-08-27     2.476388
2013-08-28     2.462865
2013-08-29     2.823951
2013-08-30     2.445771
2013-08-31     2.725194
2013-09-01     2.480032
2013-09-02     1.876902
2013-09-03     2.276983
2013-09-04     3.282450
2013-09-05     2.507296
2013-09-06     1.989307
2013-09-07     1.832832
2013-09-08     1.857287
2013-09-09     2.198939
2013-09-10     2.434254
2013-09-11     2.819172
2013-09-12     2.361830
2013-09-13     2.713215
                ...    
2014-04-01     3.163723
2014-04-02     2.861222
2014-04-03     3.212099
2014-04-04     2.832465
2014-04-05     3.283842
2014-04-06     2.824700
2014-04-07     3.455989
2014-04-08     2.995421
2014-04-09     3.780186
2014-04-10     3.703409
2014-04-11 [...]
{'fraction_non_alphanumeric': 0.09774323718427738, 'fraction_numerical': 0.26573008518905994, 'mean_word_length': 2.680968096809681, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '652479', 'license': 'Apache-2.0', 'n_tokens_mistral': 3552, 'n_tokens_neox': 2462, 'n_words': 625, 'path': 'HW4-2.ipynb', 'repo_name': 'shub911/PH256X---Python-for-research', 'size': '6691'}