我需要一些帮助。我想制作一个直方图,其中您有不同的日期(年和月)。我对月份和年份进行了排序,但是顺序不正确。有人可以帮我吗?
这是我的代码:
df["Report Date"] = pd.to_datetime(df['Report Date'])
df_gesorteerd = df.groupby(df['Report Date'].dt.strftime('%B %Y'))['Agency Id'].sum()
蜘蛛控制台为我提供以下信息:
Report Date
April 2016 70926
April 2017 69953
April 2018 67335
August 2016 71694
August 2017 74894
August 2018 69463
December 2016 67162
December 2017 68038
February 2017 63745
February 2018 62229
January 2017 66202
January 2018 69533
July 2016 66205
July 2017 71750
July 2018 65111
June 2016 69645
June 2017 68122
June 2018 61306
March 2016 88
March 2017 74489
March 2018 67672
May 2016 71183
May 2017 74714
May 2018 70341
November 2016 67342
November 2017 66511
October 2016 74240
October 2017 71187
September 2016 72979
September 2017 72342
September 2018 67844
Name: Agency Id, dtype: int64
您可以看到月份和年份的顺序不正确,我想在屏幕上看到以下代码:
Report Date
March 2016 88
April 2016 70926
June 2016 69645
...
获得这些信息后,我想制作一个直方图,在X轴上为报告日期,在Y轴上为通话次数吗?
Anayone可以帮助我吗?
答案 0 :(得分:0)
您可以使用:
df["Report Date"] = pd.to_datetime(df['Report Date'])
#sorting datetimes if necessary
df = df.sort_values('Report Date')
#parameter sort=False for avoid default sorting in groupby
dates = df['Report Date'].dt.strftime('%B %Y')
df_gesorteerd = df.groupby(dates, sort=False)['Agency Id'].sum()