Skip to content

Instantly share code, notes, and snippets.

@manmohan24nov
Created October 19, 2020 15:40
Show Gist options
  • Save manmohan24nov/e9c6cd5f82b148df0cc78f1c0fbc3ee5 to your computer and use it in GitHub Desktop.
Save manmohan24nov/e9c6cd5f82b148df0cc78f1c0fbc3ee5 to your computer and use it in GitHub Desktop.
In [2]: weekly_sales_df = sales_data[['Store',
...: 'Date',
...: 'Weekly_Sales']].groupby(['Store',
...: 'Date']).agg({'Weekly_Sales':'sum'})
In [3]: weekly_sales_df.reset_index(inplace=True)
In [4]: weekly_sales_df['Date']= pd.to_datetime(weekly_sales_df['Date'])
In [5]: weekly_sales_df = weekly_sales_df.sort_values(by='Weekly_Sales',ascending=False)
In [6]: weekly_sales_df['row num'] = weekly_sales_df.groupby(['Store']).cumcount()+1
In [7]: weekly_sales_df[weekly_sales_df['Store']==1].sort_values(by='Weekly_Sales',ascending=False)
Out[7]:
Store Date Weekly_Sales row num
112 1 2010-12-24 2387950.20 1
107 1 2011-12-23 2270188.99 2
117 1 2011-11-25 2033320.66 3
122 1 2010-11-26 1955624.11 4
25 1 2012-06-04 1899676.88 5
.. ... ... ... ...
111 1 2010-09-24 1351791.03 139
102 1 2010-10-22 1345454.00 140
95 1 2011-01-21 1327405.42 141
123 1 2012-01-27 1319325.59 142
128 1 2011-01-28 1316899.31 143
[143 rows x 4 columns]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment