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'}))
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)
In [22]:
#Use groupby and sum to aggregate all the values for each season and year
airbnb2 = airbnb2.groupby(airbnb2.index).sum()
print(airbnb2)
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)
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()