<jupyter_start><jupyter_text>Okay! We're going back to our bike path dataset here. I live in Montreal, and I was curious about whether we're more of a commuter city or a biking-for-fun city -- do people bike more on weekends, or on weekdays?  4.1 Adding a 'weekday' column to our dataframe First, we need to load up the data. We've done this before.<jupyter_code>bikes = pd.read_csv('../data/bikes.csv', sep=';', encoding='latin1', parse_dates=['Date'], dayfirst=True, index_col='Date')
bikes['Berri 1'].plot()<jupyter_output><empty_output><jupyter_text>Next up, we're just going to look at the Berri bike path. Berri is a street in Montreal, with a pretty important bike path. I use it mostly on my way to the library now, but I used to take it to work sometimes when I worked in Old Montreal. So we're going to create a dataframe with just the Berri bikepath in it<jupyter_code>berri_bikes = bikes[['Berri 1']].copy()
berri_bikes[:5]<jupyter_output><empty_output><jupyter_text>Next, we need to add a 'weekday' column. Firstly, we can get the weekday from the index. We haven't talked about indexes yet, but the index is what's on the left on the above dataframe, under 'Date'. It's basically all the days of the year.<jupyter_code>berri_bikes.index[:4]
my_timestamp = berri_bikes.index[5]
my_timestamp, my_timestamp.day_name(), my_timestamp.dayofweek, my_timestamp.is_leap_year<jupyter_output><empty_output><jupyter_text>You can see that actually some of the days are missing -- only 310 days of the year are actually there. Who knows why.Pandas has a bunch of really great time series functionality, so if we wanted to get the day of the month for each row, we could do it like this:<jupyter_code>berri_bikes.index.day<jupyter_output><empty_output><jupyter_text>We actually want the weekday, though:<jupyter_code>berri_bikes.index.weekday<jupyter_output><empty_output><jupyter_text>These are the days of the week, where 0 is Monday. I found out that 0 was Monday by checking on a calendar.Now that we know how to *get* the weekday, we can add it as a column in our dataframe like this:<jupyter_code>berri_bikes.loc[:,'weekday'] = berri_bikes.index.weekday
berri_bikes[5:10]<jupyter_output><empty_output><jupyter_text>4.2 Adding up the cyclists by weekday This turns out to be really easy!Dataframes have a `.groupby()` method that is similar to SQL groupby, if you're familiar with that. I'm not going to explain more about it right now -- if you want to to know more, [the documentation](http://pandas.pydata.org/pandas-docs/stable/groupby.html) is really good.In this case, `berri_bikes.groupby('weekday').aggregate(sum)` means "Group the rows by weekday and then add up all the values with the same weekday".<jupyter_code>weekday_counts = berri_bikes.groupby('weekday').aggregate(sum)
weekday_counts<jupyter_output><empty_output><jupyter_text>It's hard to remember what 0, 1, 2, 3, 4, 5, 6 mean, so we can fix it up and graph it:<jupyter_code>weekday_counts.index = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
weekday_counts
weekday_counts.plot(kind='bar')<jupyter_output><empty_output><jupyter_text>So it looks like Montrealers are commuter cyclists -- they bike much more during the week. Neat!  4.3 Putting it together Let's put all that together, to prove how easy it is. 6 lines of magical pandas!If you want to play around, try changing `sum` to `max`, `numpy.median`, or any other function you like.<jupyter_code>bikes = pd.read_csv('../data/bikes.csv', 
                    sep=';', encoding='latin1', 
                    parse_dates=['Date'], dayfirst=True, 
                    index_col='Date')
# Add the weekday column
berri_bikes = bikes[['Berri 1']].copy()
berri_bikes.loc[:,'weekday'] = berri_bikes.index.weekday

# Add up the number of cyclists by weekday, and plot!
weekday_counts = berri_bikes.groupby('weekday').aggregate(sum)
weekday_counts.index = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
weekday_counts.plot(kind='bar')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10417180942977043, 'fraction_numerical': 0.00740557886941496, 'mean_word_length': 5.33125, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 0, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '26452', 'license': 'MIT', 'n_tokens_mistral': 1362, 'n_tokens_neox': 1290, 'n_words': 473, 'path': 'contents/introduction-to-pandas/Chapter 4 - Find out on which weekday people bike the most with groupby and aggregate.ipynb', 'repo_name': 'spu-dataviz-211/class-materials', 'size': '4051'}