You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
| 0 | Albany Park | 5150 N. Kimball Ave. | Chicago | 60625.0 | 120059 | 2011 | january | 8427 |
39
-
| 1 | Altgeld | 13281 S. Corliss Ave. | Chicago | 60827.0 | 9611 | 2011 | january | 1258 |
40
-
| 2 | Archer Heights | 5055 S. Archer Ave. | Chicago | 60632.0 | 101951 | 2011 | january | 8104 |
41
-
| 3 | Austin | 5615 W. Race Ave. | Chicago | 60644.0 | 25527 | 2011 | january | 1755 |
42
-
| 4 | Austin-Irving | 6100 W. Irving Park Rd. | Chicago | 60634.0 | 165634 | 2011 | january | 12593 |
43
-
44
-
## Convert year and month to datetime
45
-
46
-
In order to plot this data over time we need to do two things to prepare it first. First, we need to combine the year and month columns into a single [datetime](https://docs.python.org/3/library/datetime.html) column using the Pandas `to_datetime` function. Second, we assign the date column as our index for the data. These two steps will set up our data for plotting.
-`df_long['date']` - First, we create a new `date` column.
55
-
-`pd.to_datetime()` - Next we package everything into a datetime object.
56
-
-`df_long['year'].astype(str)` - We use the `.astype(str)` method to convert the year column to a string
57
-
-`+ '-' + df_long['month'],` - We concatenate a `-` to the string as a separator, followed by the month column.
58
-
-`format='%Y-%B'` - We pass the datetime parameter to tell Python to expect a 4 digit year (%Y), followed by a dash, followed by the month's full name (%B).
59
-
60
-
If we take a look at the date column, we'll see that datetime automatically adds a day (always `01`) in the absence of any specific day input.
That worked! Now, we can make the datetime column the index of our DataFrame. In the Pandas episode we looked at Pandas default numerical index, but we can also use `.set_index()` to declare a specific column as the index of our DataFrame. Using a datetime index will make it easier for us to plot the DataFrame over time. The first parameter of `.set_index()` is the column name and the `inplace=True` parameter allows us to modify the DataFrame without assigning it to a new variable.
104
-
105
-
106
-
```python
107
-
df_long.set_index('date', inplace=True)
108
-
```
109
-
110
-
If we look at the data again, we will see our index will be set to date.
111
-
112
-
```python
113
-
df_long.head()
114
-
```
115
-
116
36
|| branch | address | city | zip code | ytd | year | month | circulation |
0 commit comments