(c) 2016 - present. Enplus Advisors, Inc.
import numpy as np
import pandas as pd
pd.set_option('display.precision', 2)
Data
sp5_jan
is SP500 market close prices and trading volume for
January 2015.sales
is weekly sales data for Acme Widgets Co. for January
2015 in thousands of widgets sold and $ millions in revenuenp.random.seed(100)
sp5 = pd.read_csv(
'sp500.csv', parse_dates=['date'], index_col=['date'],
usecols=['date', 'close', 'volume'])\
.sort_index()
sp5_jan = sp5.loc['2015-01', :].copy()
sp5_jan['volume'] = sp5_jan['volume'] / 1e6
sales = pd.DataFrame({
'date': pd.date_range('2015-01-01', '2015-01-31', freq='W'),
})
sales['widgets_sold'] = abs(10 * np.random.randn(sales.shape[0])).round()
sales['revenue'] = sales.widgets_sold * 20
Exercise:
Merge sp5_jan
with sales
, filling sales data forward. Save
the result as res_1
. Your result should have the same number of records
as sp5_jan
.
Exercise:
Convert the output from the previous exercise to long format with
date
as the ID variable, saving the result as res_2
Exercise
Convert res_2
back to wide format using the pivot
method.
Exercise
Convert res_2
back to wide format using the unstack
method.