Permalink
Browse files

adding a new notebook

  • Loading branch information...
Aidan Morrison
Aidan Morrison committed Dec 2, 2019
1 parent 29ecc58 commit 29299f9c74c7c9c07851e85c8e5de48ac7314c30
Showing with 231 additions and 0 deletions.
  1. +231 −0 Untitled3.ipynb
@@ -0,0 +1,231 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"df = pd.DataFrame({'Col1': [10, 20, 15, 30, 45],\n",
" 'Col2': [13, 23, 18, 33, 48],\n",
" 'Col3': [17, 27, 22, 37, 52]})"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(5, 3)"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.shift(1).shape\n",
"df.shape"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"def multilagger(df, lag_list):\n",
" bound_df = df\n",
" for i in lag_list:\n",
" new_df = df.shift(i)\n",
" new_df.columns = df.columns + \"_lag_\" + str(i)\n",
" bound_df = pd.concat([bound_df, new_df], axis = 1)\n",
" \n",
" return bound_df\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Col1</th>\n",
" <th>Col2</th>\n",
" <th>Col3</th>\n",
" <th>Col1_lag_-1</th>\n",
" <th>Col2_lag_-1</th>\n",
" <th>Col3_lag_-1</th>\n",
" <th>Col1_lag_-2</th>\n",
" <th>Col2_lag_-2</th>\n",
" <th>Col3_lag_-2</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>10</td>\n",
" <td>13</td>\n",
" <td>17</td>\n",
" <td>20.0</td>\n",
" <td>23.0</td>\n",
" <td>27.0</td>\n",
" <td>15.0</td>\n",
" <td>18.0</td>\n",
" <td>22.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>20</td>\n",
" <td>23</td>\n",
" <td>27</td>\n",
" <td>15.0</td>\n",
" <td>18.0</td>\n",
" <td>22.0</td>\n",
" <td>30.0</td>\n",
" <td>33.0</td>\n",
" <td>37.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>15</td>\n",
" <td>18</td>\n",
" <td>22</td>\n",
" <td>30.0</td>\n",
" <td>33.0</td>\n",
" <td>37.0</td>\n",
" <td>45.0</td>\n",
" <td>48.0</td>\n",
" <td>52.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>30</td>\n",
" <td>33</td>\n",
" <td>37</td>\n",
" <td>45.0</td>\n",
" <td>48.0</td>\n",
" <td>52.0</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>45</td>\n",
" <td>48</td>\n",
" <td>52</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Col1 Col2 Col3 Col1_lag_-1 Col2_lag_-1 Col3_lag_-1 Col1_lag_-2 \\\n",
"0 10 13 17 20.0 23.0 27.0 15.0 \n",
"1 20 23 27 15.0 18.0 22.0 30.0 \n",
"2 15 18 22 30.0 33.0 37.0 45.0 \n",
"3 30 33 37 45.0 48.0 52.0 NaN \n",
"4 45 48 52 NaN NaN NaN NaN \n",
"\n",
" Col2_lag_-2 Col3_lag_-2 \n",
"0 18.0 22.0 \n",
"1 33.0 37.0 \n",
"2 48.0 52.0 \n",
"3 NaN NaN \n",
"4 NaN NaN "
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"multilagger(df, [-1,-2])"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"check = df.columns"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Index(['Col11', 'Col21', 'Col31'], dtype='object')"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"check + \"_1\""
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit 29299f9

Please sign in to comment.