Skip to content

Instantly share code, notes, and snippets.

@Everfighting
Last active July 12, 2022 03:00
Show Gist options
  • Save Everfighting/6dc4d5c79a5ba7392f3781983dff1f1a to your computer and use it in GitHub Desktop.
Save Everfighting/6dc4d5c79a5ba7392f3781983dff1f1a to your computer and use it in GitHub Desktop.
import pandas as pd
from pandas.api.types import CategoricalDtype
df = pd.DataFrame({
'order_id': [1001, 1002, 1003, 1004, 1005, 1006, 1007],
'customer_id': [10, 12, 12, 12, 10, 10, 10],
'month': ['Feb', 'Jan', 'Jan', 'Feb', 'Feb', 'Jan', 'Feb'],
'day_of_week': ['Mon', 'Wed', 'Sun', 'Tue', 'Sat', 'Mon', 'Thu'],
})
cat_day_of_week = CategoricalDtype(
['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
ordered=True
)
cat_month = CategoricalDtype(
['Jan', 'Feb', 'Mar', 'Apr'],
ordered=True,
)
df['day_of_week'] = df['day_of_week'].astype(cat_day_of_week)
df['month'] = df['month'].astype(cat_month)
df.sort_values(['month', 'day_of_week'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment