Created
May 5, 2017 01:11
-
-
Save youngsoul/a1bb592b3cde30eff8cab58b8eba119c to your computer and use it in GitHub Desktop.
Create a categorical column order for the Python for Data Science and machine learning class on Udemy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# create a collection of the ordered months | |
all_months = ['January', 'February', 'March', 'April','May','June','July','August','September', 'October', 'November', 'December'] | |
# create a categorical column from the month column | |
flights['ordered_month'] = pd.Categorical(flights['month'], all_months) | |
flights.head() | |
fp = flights.pivot_table(index='ordered_month', columns='year', values='passengers') | |
sns.heatmap(fp) | |
# We could also create the DataFrame with the columns sorted like: | |
# something like: | |
ordered_months_dict = {'January':0,'February':1, 'March': 2, 'April': 3 } # etc | |
ordered_flights = pd.DataFrame(flights, columns=sorted(custom_dict, key=custom_dict.get)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment