下面的数据框为我提供了以下结果,其中我对前四个执行“ source_id”的结果进行了过滤
df= highestrevenue.loc[highestrevenue['source_id'].isin(['PA0202','PA0057','PA0873','PA0678'])]
print(df)
date source_id cost revenue
3322 2014-01-11 PA0202 9637.0 12000.0
3176 2014-01-17 PA0057 1691.0 11999.0
4748 2014-10-14 PA0873 8376.0 11995.0
752 2014-05-30 PA0678 9739.0 11992.0
5442 2014-02-13 PA0202 4157.0 11954.0
... ... ... ... ...
9800 2014-02-09 PA0873 989.0 NaN
9855 2014-06-20 PA0873 6407.0 NaN
9857 2014-03-01 PA0202 7104.0 NaN
9897 2014-07-14 PA0057 7231.0 NaN
9946 2014-01-08 PA0057 2308.0 NaN
[789 rows x 4 columns]
我希望日期列以月份为单位,这样我就可以得出每个月的总收入
df_plot.groupby(df_plot['date'].dt.strftime('%B'))['revenue'].sum().sort_values()
那给我
date
February 123702.0
April 136110.0
July 145350.0
March 178350.0
October 199992.0
September 203631.0
December 204183.0
January 209337.0
August 231515.0
November 233001.0
May 267656.0
June 277374.0
但是,我想要3列,分别是按月的日期,Revenue和source_id(4),以便进一步制作线形图,其中x轴为月份,y轴为收入,其中4根线显示了4个源ID
现在,我如何获得上述数据框中的第三列,即source_id ???
预期输出数据帧为
date Revenue source_id
您必须对日期和ID进行分组:
(df_plot.groupby([df_plot['date'].dt.strftime('%B'), 'source_id'])['revenue']
.sum()
.sort_index())
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句