Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save youngsoul/a1bb592b3cde30eff8cab58b8eba119c to your computer and use it in GitHub Desktop.
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
# 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