-
Geographical Analysis 6
-
Lecture1.1
-
Lecture1.2
-
Lecture1.3
-
Lecture1.4
-
Lecture1.5
-
Lecture1.6
-
-
Cap Table 3
-
Lecture2.1
-
Lecture2.2
-
Lecture2.3
-
-
Simulation 6
-
Lecture3.1
-
Lecture3.2
-
Lecture3.3
-
Lecture3.4
-
Lecture3.5
-
Lecture3.6
-
-
Search Index 8
-
Lecture4.1
-
Lecture4.2
-
Lecture4.3
-
Lecture4.4
-
Lecture4.5
-
Lecture4.6
-
Lecture4.7
-
Lecture4.8
-
-
Fund Distributions 5
-
Lecture5.1
-
Lecture5.2
-
Lecture5.3
-
Lecture5.4
-
Lecture5.5
-
Mapping
Mapping¶
The idea of mapping, taking one set of values and transferring it to another set of values based on some function or translation dictionary, is important for what we are going to do next. Instead of just simply looking at months, we want to map each month to the season it is in, and then from there see what the seasonality might look like. We call the function map on our index, and pass a dictionary denoting what value each key sould get. The output is the season associated with each index.
In [20]:
#An example of how it works
print(airbnb2.index.map({1: 'Winter',
2: 'Winter',
3: 'Spring',
4: 'Spring',
5: 'Spring',
6: 'Summer',
7: 'Summer',
8: 'Summer',
9: 'Fall',
10: 'Fall',
11: 'Fall',
12: 'Winter'}))
Index(['Winter', 'Winter', 'Spring', 'Spring', 'Spring', 'Summer', 'Summer',
'Summer', 'Fall', 'Fall', 'Fall', 'Winter'],
dtype='object', name='Month')
In [21]:
#Change the index to be the mapped version
airbnb2.index = airbnb2.index.map({1: 'Winter',
2: 'Winter',
3: 'Spring',
4: 'Spring',
5: 'Spring',
6: 'Summer',
7: 'Summer',
8: 'Summer',
9: 'Fall',
10: 'Fall',
11: 'Fall',
12: 'Winter'})
print(airbnb2)
Year 2012 2013 2014 2015 2016 2017 2018 \
Month
Winter 0.037037 0.056075 0.061033 0.055710 0.067194 0.073276 0.083863
Winter 0.055556 0.056075 0.056338 0.061281 0.071146 0.077586 0.082592
Spring 0.074074 0.074766 0.070423 0.064067 0.075099 0.084770 0.090216
Spring 0.074074 0.074766 0.084507 0.077994 0.077075 0.086207 0.083863
Spring 0.092593 0.093458 0.084507 0.094708 0.096838 0.089080 0.087675
Summer 0.092593 0.093458 0.093897 0.103064 0.114625 0.097701 0.100381
Summer 0.092593 0.102804 0.117371 0.105850 0.098814 0.103448 0.102922
Summer 0.111111 0.102804 0.103286 0.105850 0.090909 0.093391 0.088945
Fall 0.092593 0.084112 0.089202 0.086351 0.079051 0.077586 0.076239
Fall 0.092593 0.093458 0.089202 0.083565 0.077075 0.074713 0.069886
Fall 0.092593 0.084112 0.075117 0.077994 0.077075 0.071839 0.064803
Winter 0.092593 0.084112 0.075117 0.083565 0.075099 0.070402 0.068615
Year 2019
Month
Winter 0.079383
Winter 0.077178
Spring 0.082690
Spring 0.080485
Spring 0.088203
Summer 0.102536
Summer 0.101433
Summer 0.091510
Fall 0.079383
Fall 0.076075
Fall 0.070562
Winter 0.070562
In [22]:
#Use groupby and sum to aggregate all the values for each season and year
airbnb2 = airbnb2.groupby(airbnb2.index).sum()
print(airbnb2)
Year 2012 2013 2014 2015 2016 2017 2018 \
Month
Fall 0.277778 0.261682 0.253521 0.247911 0.233202 0.224138 0.210928
Spring 0.240741 0.242991 0.239437 0.236769 0.249012 0.260057 0.261753
Summer 0.296296 0.299065 0.314554 0.314763 0.304348 0.294540 0.292249
Winter 0.185185 0.196262 0.192488 0.200557 0.213439 0.221264 0.235070
Year 2019
Month
Fall 0.226020
Spring 0.251378
Summer 0.295480
Winter 0.227122
Also, let's re-arrange the index so that it starts with winter.
In [23]:
airbnb2 = airbnb2.reindex(index=['Winter', 'Spring', 'Summer', 'Fall'])
print(airbnb2)
Year 2012 2013 2014 2015 2016 2017 2018 \
Month
Winter 0.185185 0.196262 0.192488 0.200557 0.213439 0.221264 0.235070
Spring 0.240741 0.242991 0.239437 0.236769 0.249012 0.260057 0.261753
Summer 0.296296 0.299065 0.314554 0.314763 0.304348 0.294540 0.292249
Fall 0.277778 0.261682 0.253521 0.247911 0.233202 0.224138 0.210928
Year 2019
Month
Winter 0.227122
Spring 0.251378
Summer 0.295480
Fall 0.226020
In [24]:
ax = airbnb2.plot(kind='line')
ax.set_xlabel("Season")
ax.set_ylabel("% of Yearly Search Index")
ax.set_title("Google Search Index Airbnb Season Comparison")
ax.yaxis.set_major_formatter(PercentFormatter(1))
plt.show()
/Users/seanmcowen/anaconda/envs/FinanceAndPython/lib/python3.8/site-packages/pandas/plotting/_matplotlib/core.py:1192: UserWarning: FixedFormatter should only be used together with FixedLocator
ax.set_xticklabels(xticklabels)
Prev
Prediction
Next
Regression