我正在尝试从1月到12月订购条形图,而不是从最高价的最低价开始订购,我敢肯定有一个简单的解决方法,但我找不到它。目前,我的图形在值上进行排序,而不是在x轴上进行排序。我在代码的第一行添加了month列。第二秒钟我发现了每个月的计数。我在条形图中绘制的第三个。
df1['Month Added'] = pd.DatetimeIndex(df1['Date Added']).month_name()
df_month = df1['Month Added'].value_counts()
df_month.plot(kind='bar')
我尝试了以下方法,但是没有运气。
df_month.Month.value_counts().loc[['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']].plot(kind='bar')
pandas.Series.sort_index
与key
和一起使用pandas.to_datetime
:
df_month = df_month.sort_index(key=lambda x: pd.to_datetime(x, format="%B"))
print(df_month)
输出:
January 25
February 21
March 14
April 5
May 30
June 16
July 8
August 30
September 6
October 9
November 36
December 24
dtype: int64
然后绘制:
df_month.plot(kind="bar")
输出:
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句