Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- import datetime as dt
- import numpy as np
- import matplotlib.pyplot as plt
- from pandas.plotting import register_matplotlib_converters
- import warnings
- # конвертеры, которые позволяют использовать типы pandas в matplotlib
- register_matplotlib_converters()
- orders = pd.read_csv('/datasets/data_for_tasks_3.csv', sep=',')
- orders['date'] = orders['date'].map(
- lambda x: dt.datetime.strptime(x, '%d/%m/%Y')
- )
- visitors = pd.read_csv('/datasets/data_for_tasks_3_visitors.csv', sep=',')
- visitors['date'] = visitors['date'].map(
- lambda x: dt.datetime.strptime(x, '%d/%m/%Y')
- )
- datesGroups = orders[['date', 'group']].drop_duplicates()
- ordersAggregated = datesGroups.apply(
- lambda x: orders[
- np.logical_and(
- orders['date'] <= x['date'], orders['group'] == x['group']
- )
- ].agg(
- {
- 'date': 'max',
- 'group': 'max',
- 'orderId': 'nunique',
- 'userId': 'nunique',
- 'revenue': 'sum',
- }
- ),
- axis=1,
- ).sort_values(by=['date', 'group'])
- visitorsAggregated = datesGroups.apply(
- lambda x: visitors[
- np.logical_and(
- visitors['date'] <= x['date'], visitors['group'] == x['group']
- )
- ].agg({'date': 'max', 'group': 'max', 'visitors': 'sum'}),
- axis=1,
- ).sort_values(by=['date', 'group'])
- cumulativeData = ordersAggregated.merge(
- visitorsAggregated, left_on=['date', 'group'], right_on=['date', 'group']
- )
- cumulativeData.columns = [
- 'date',
- 'group',
- 'orders',
- 'buyers',
- 'revenue',
- 'visitors',
- ]
- cumulativeRevenueA = cumulativeData[cumulativeData['group'] == 'A'][
- ['date', 'revenue', 'orders']
- ]
- cumulativeRevenueB = cumulativeData[cumulativeData['group'] == 'B'][
- ['date', 'revenue', 'orders']
- ]
- mergedCumulativeRevenue = cumulativeRevenueA.merge(cumulativeRevenueB, left_on = 'date',
- right_on='date', how='left',
- suffixes = ['A', 'B'])
- plt.figure(figsize=(12,4))
- plt.plot(mergedCumulativeRevenue['date'],
- (mergedCumulativeRevenue['revenueB']/mergedCumulativeRevenue['ordersB'] -
- mergedCumulativeRevenue['revenueA']/mergedCumulativeRevenue['ordersA'])/(
- mergedCumulativeRevenue['revenueA']/mergedCumulativeRevenue['ordersA']))
- plt.axhline(y = 0, color ="black", linestyle ="--")
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement