October 2019
Beginner to intermediate
674 pages
16h 43m
English
In order to do the same thing in pandas, we read the CSV file saved in the preceding section:
In [40]: flights_sample=pd.read_csv('nycflights13_sample.csv')
In [41]: flights_sample.head()
Out[41]: year month dep_delayarr_delay distance
0 2013 3 2 5 184
1 2013 1 0 4 762
2 2013 11 -7 -27 509
3 2013 5 -5 -12 184
4 2013 8 -1 -10 937
We achieve the same effect as ddply by making use of the GroupBy() operator, as shown in the following code and output:
In [44]: pd.set_option('precision',3)
In [45]: grouped = flights_sample_df.groupby(['year','month'])
In [48]: grouped['dep_delay'].agg([np.mean, np.std])
Out[48]: mean std
year month
2013 1 -0.20 2.28
2 23.85 61.63
3 10.00 34.72
4 0.88 12.56
5 8.56 32.42
6 58.14 ...Read now
Unlock full access