Skip to content

Instantly share code, notes, and snippets.

@karamanbk
Last active January 3, 2022 09:20
Show Gist options
  • Save karamanbk/d0477bf8cff17c1732eac13cd4a840c8 to your computer and use it in GitHub Desktop.
Save karamanbk/d0477bf8cff17c1732eac13cd4a840c8 to your computer and use it in GitHub Desktop.
#converting the type of Invoice Date Field from string to datetime.
tx_data['InvoiceDate'] = pd.to_datetime(tx_data['InvoiceDate'])
#creating YearMonth field for the ease of reporting and visualization
tx_data['InvoiceYearMonth'] = tx_data['InvoiceDate'].map(lambda date: 100*date.year + date.month)
#calculate Revenue for each row and create a new dataframe with YearMonth - Revenue columns
tx_data['Revenue'] = tx_data['UnitPrice'] * tx_data['Quantity']
tx_revenue = tx_data.groupby(['InvoiceYearMonth'])['Revenue'].sum().reset_index()
tx_revenue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment