<jupyter_start><jupyter_text>Aggregating sales by month<jupyter_code>train.date_block_num.describe()
last_month_sales = train.loc[train.date_block_num == 33]
last_month_sales.reset_index(inplace=True)
print last_month_sales.shape
last_month_sales.head()
last_month_sales.pop('date')
last_month_sales.pop('date_block_num')
last_month_sales.pop('item_price')
last_month_sales.pop('index')
last_month_sales.head()
last_month_sales.item_cnt_day.describe()
last_month_sales = last_month_sales.groupby(['shop_id', 'item_id'], as_index=False).agg({'item_cnt_day':sum})
#last_month_sales['total'] = last_month_sales.groupby(['shop_id', 'item_id'])['item_cnt_day'].transform('sum')
print last_month_sales.shape
last_month_sales.head()
last_month_sales.item_cnt_day.describe()
#last_month_sales.drop_duplicates(subset=['shop_id', 'item_id'], inplace=True)
print last_month_sales.shape
last_month_sales.head()
test.head()
last_month_sales['total'] = last_month_sales['item_cnt_day']
last_month_sales.head()
t = last_month_sales.loc[(last_month_sales.shop_id == 45) & (last_month_sales.item_id==13881)]
t.total.values[0]
test_y = np.zeros(shape=test.shape[0])
print test_y.shape
print test.shape[0]
for i in range(test.shape[0]):
    id0 = test['ID'][i]
    shop_id0 = test['shop_id'][i]
    item_id0 = test['item_id'][i]
    #print id0, shop_id0, item_id0
    if shop_id0 in last_month_sales.shop_id.values:
        possibles = last_month_sales.loc[last_month_sales.shop_id == shop_id0]
        if item_id0 in possibles.item_id.values: 
            t = possibles.loc[possibles.item_id==item_id0]
            #if t.shape[0] > 0:
            test_y[id0] = t.total.values[0]
            #else:
            #    test_y[id0] = 0.0
        else:
            test_y[id0] = 0.0
    else:
        test_y[id0] = 0.0
test_id = test.ID
test_submit = pd.DataFrame({'ID': test_id, 'item_cnt_month': test_y})
test_submit.describe()
test_submit.item_cnt_month[test_submit.item_cnt_month > 20] = 20
test_submit.item_cnt_month[test_submit.item_cnt_month < 0] = 0
test_submit.describe()
print test_submit.shape
test_submit.to_csv('last_month_sales.csv', index=False)
test_submit.head()<jupyter_output>(214200, 2)
{'fraction_non_alphanumeric': 0.13748854262144822, 'fraction_numerical': 0.022456461961503207, 'mean_word_length': 5.555555555555555, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '499654', 'license': 'MIT', 'n_tokens_mistral': 895, 'n_tokens_neox': 839, 'n_words': 111, 'path': 'script/EDA.ipynb', 'repo_name': 'maviator/kaggle_coursera_course', 'size': '2182'}