首先,如果可能缺少某些月份,则可以使用 ordered categoricals 与月份列表中的所有类别:
months = ['January','February','March','April',
'May','June','July','August',
'September','October','November','December']
months = df_Badge.Date.dt.month_name().value_counts()
months.index = pd.CategoricalIndex(months.index, ordered=True, categories=months)
months = months.sort_index()
sns.barplot(months.index, months.values, alpha=0.8)
如果索引中的所有月份都可能使用Series.reindex,如果缺少某个月份,则添加缺失值:
months = ['January','February','March','April',
'May','June','July','August',
'September','October','November','December']
months = df_Badge.Date.dt.month_name().value_counts().reindex(months)
sns.barplot(months.index, months.values, alpha=0.8)