1

The below dataframe gives me the following results where I have filtered on the top 4 performing '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]

I wanted the date column to be organized in months which gives me sum of revenue for each month, I have used

df_plot.groupby(df_plot['date'].dt.strftime('%B'))['revenue'].sum().sort_values()

that gives me

 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

But, I wanted 3 columns, date by months, Revenue and source_id(4) so that further I am able to make a line graph , with x-axis as months, y-axis as revenue and 4 lines showing the 4 source id's

for now, how do i get the third column thats the source_id in in the above dataframe???

expected output dataframe is

date  Revenue  source_id

1 Answer 1

1

You have to group on both date and ID:

(df_plot.groupby([df_plot['date'].dt.strftime('%B'), 'source_id'])['revenue']
        .sum()
        .sort_index())
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.