{
"cells": [
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Patron Type Code | \n",
" Patron Type Definition | \n",
" Total Checkouts | \n",
" Total Renewals | \n",
" Age Range | \n",
" Home Library Code | \n",
" Home Library Definition | \n",
" Circulation Active Month | \n",
" Circulation Active Year | \n",
" Notice Preference Code | \n",
" Notice Preference Definition | \n",
" Provided Email Address | \n",
" Year Patron Registered | \n",
" Outside of County | \n",
" Supervisor District | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 0 | \n",
" ADULT | \n",
" 1092 | \n",
" 761 | \n",
" 60 to 64 years | \n",
" M6 | \n",
" Mission | \n",
" July | \n",
" 2016 | \n",
" p | \n",
" phone | \n",
" False | \n",
" 2003 | \n",
" False | \n",
" 9.0 | \n",
"
\n",
" \n",
" 1 | \n",
" 0 | \n",
" ADULT | \n",
" 0 | \n",
" 0 | \n",
" 20 to 24 years | \n",
" P1 | \n",
" Park | \n",
" None | \n",
" None | \n",
" z | \n",
" email | \n",
" True | \n",
" 2014 | \n",
" False | \n",
" 5.0 | \n",
"
\n",
" \n",
" 2 | \n",
" 0 | \n",
" ADULT | \n",
" 31 | \n",
" 22 | \n",
" 25 to 34 years | \n",
" S7 | \n",
" Sunset | \n",
" April | \n",
" 2016 | \n",
" z | \n",
" email | \n",
" True | \n",
" 2010 | \n",
" False | \n",
" 4.0 | \n",
"
\n",
" \n",
" 3 | \n",
" 0 | \n",
" ADULT | \n",
" 0 | \n",
" 0 | \n",
" 45 to 54 years | \n",
" P1 | \n",
" Park | \n",
" None | \n",
" None | \n",
" a | \n",
" print | \n",
" False | \n",
" 2016 | \n",
" True | \n",
" NaN | \n",
"
\n",
" \n",
" 4 | \n",
" 0 | \n",
" ADULT | \n",
" 0 | \n",
" 0 | \n",
" 25 to 34 years | \n",
" X | \n",
" Main Library | \n",
" None | \n",
" None | \n",
" z | \n",
" email | \n",
" True | \n",
" 2015 | \n",
" False | \n",
" 3.0 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Patron Type Code Patron Type Definition Total Checkouts Total Renewals \\\n",
"0 0 ADULT 1092 761 \n",
"1 0 ADULT 0 0 \n",
"2 0 ADULT 31 22 \n",
"3 0 ADULT 0 0 \n",
"4 0 ADULT 0 0 \n",
"\n",
" Age Range Home Library Code Home Library Definition \\\n",
"0 60 to 64 years M6 Mission \n",
"1 20 to 24 years P1 Park \n",
"2 25 to 34 years S7 Sunset \n",
"3 45 to 54 years P1 Park \n",
"4 25 to 34 years X Main Library \n",
"\n",
" Circulation Active Month Circulation Active Year Notice Preference Code \\\n",
"0 July 2016 p \n",
"1 None None z \n",
"2 April 2016 z \n",
"3 None None a \n",
"4 None None z \n",
"\n",
" Notice Preference Definition Provided Email Address \\\n",
"0 phone False \n",
"1 email True \n",
"2 email True \n",
"3 print False \n",
"4 email True \n",
"\n",
" Year Patron Registered Outside of County Supervisor District \n",
"0 2003 False 9.0 \n",
"1 2014 False 5.0 \n",
"2 2010 False 4.0 \n",
"3 2016 True NaN \n",
"4 2015 False 3.0 "
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pandas as pd\n",
"\n",
"df = pd.read_csv(\"../data/Library_Usage.csv\")\n",
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 2016.0\n",
"1 NaN\n",
"2 2016.0\n",
"3 NaN\n",
"4 NaN\n",
" ... \n",
"423443 2015.0\n",
"423444 2016.0\n",
"423445 2016.0\n",
"423446 2015.0\n",
"423447 NaN\n",
"Name: Circulation Active Year, Length: 423448, dtype: float64"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df['Circulation Active Year'] = pd.to_numeric(\n",
" df['Circulation Active Year'], errors='coerce'\n",
")\n",
"df['Circulation Active Year']"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 1900-07-01\n",
"1 NaT\n",
"2 1900-04-01\n",
"3 NaT\n",
"4 NaT\n",
" ... \n",
"423443 1900-03-01\n",
"423444 1900-07-01\n",
"423445 1900-07-01\n",
"423446 1900-04-01\n",
"423447 NaT\n",
"Name: Circulation Active Month, Length: 423448, dtype: datetime64[ns]"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df['Circulation Active Month'] = pd.to_datetime(\n",
" df['Circulation Active Month'],\n",
" errors='coerce',\n",
" format=\"%B\"\n",
")\n",
"df['Circulation Active Month']"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 7.0\n",
"1 NaN\n",
"2 4.0\n",
"3 NaN\n",
"4 NaN\n",
" ... \n",
"423443 3.0\n",
"423444 7.0\n",
"423445 7.0\n",
"423446 4.0\n",
"423447 NaN\n",
"Name: Circulation Active Month, Length: 423448, dtype: float64"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df['Circulation Active Month'] = df['Circulation Active Month'].dt.month\n",
"df['Circulation Active Month']"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"df['Membership Duration'] = (\n",
" df['Circulation Active Year'] - df['Year Patron Registered']\n",
")*12 + df['Circulation Active Month']"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 163.0\n",
"1 0.0\n",
"2 76.0\n",
"3 0.0\n",
"4 0.0\n",
" ... \n",
"423443 75.0\n",
"423444 7.0\n",
"423445 31.0\n",
"423446 148.0\n",
"423447 0.0\n",
"Name: Membership Duration, Length: 423448, dtype: float64"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df['Membership Duration'] = df['Membership Duration'].fillna(0)\n",
"df['Membership Duration']"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Patron Type Code | \n",
" Patron Type Definition | \n",
" Total Checkouts | \n",
" Total Renewals | \n",
" Age Range | \n",
" Home Library Code | \n",
" Home Library Definition | \n",
" Circulation Active Month | \n",
" Circulation Active Year | \n",
" Notice Preference Code | \n",
" Notice Preference Definition | \n",
" Provided Email Address | \n",
" Year Patron Registered | \n",
" Outside of County | \n",
" Supervisor District | \n",
" Membership Duration | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 0 | \n",
" ADULT | \n",
" 1092 | \n",
" 761 | \n",
" 60 to 64 years | \n",
" M6 | \n",
" Mission | \n",
" 7.0 | \n",
" 2016.0 | \n",
" p | \n",
" phone | \n",
" False | \n",
" 2003 | \n",
" False | \n",
" 9.0 | \n",
" 163.0 | \n",
"
\n",
" \n",
" 1 | \n",
" 0 | \n",
" ADULT | \n",
" 0 | \n",
" 0 | \n",
" 20 to 24 years | \n",
" P1 | \n",
" Park | \n",
" NaN | \n",
" NaN | \n",
" z | \n",
" email | \n",
" True | \n",
" 2014 | \n",
" False | \n",
" 5.0 | \n",
" 0.0 | \n",
"
\n",
" \n",
" 2 | \n",
" 0 | \n",
" ADULT | \n",
" 31 | \n",
" 22 | \n",
" 25 to 34 years | \n",
" S7 | \n",
" Sunset | \n",
" 4.0 | \n",
" 2016.0 | \n",
" z | \n",
" email | \n",
" True | \n",
" 2010 | \n",
" False | \n",
" 4.0 | \n",
" 76.0 | \n",
"
\n",
" \n",
" 3 | \n",
" 0 | \n",
" ADULT | \n",
" 0 | \n",
" 0 | \n",
" 45 to 54 years | \n",
" P1 | \n",
" Park | \n",
" NaN | \n",
" NaN | \n",
" a | \n",
" print | \n",
" False | \n",
" 2016 | \n",
" True | \n",
" NaN | \n",
" 0.0 | \n",
"
\n",
" \n",
" 4 | \n",
" 0 | \n",
" ADULT | \n",
" 0 | \n",
" 0 | \n",
" 25 to 34 years | \n",
" X | \n",
" Main Library | \n",
" NaN | \n",
" NaN | \n",
" z | \n",
" email | \n",
" True | \n",
" 2015 | \n",
" False | \n",
" 3.0 | \n",
" 0.0 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Patron Type Code Patron Type Definition Total Checkouts Total Renewals \\\n",
"0 0 ADULT 1092 761 \n",
"1 0 ADULT 0 0 \n",
"2 0 ADULT 31 22 \n",
"3 0 ADULT 0 0 \n",
"4 0 ADULT 0 0 \n",
"\n",
" Age Range Home Library Code Home Library Definition \\\n",
"0 60 to 64 years M6 Mission \n",
"1 20 to 24 years P1 Park \n",
"2 25 to 34 years S7 Sunset \n",
"3 45 to 54 years P1 Park \n",
"4 25 to 34 years X Main Library \n",
"\n",
" Circulation Active Month Circulation Active Year Notice Preference Code \\\n",
"0 7.0 2016.0 p \n",
"1 NaN NaN z \n",
"2 4.0 2016.0 z \n",
"3 NaN NaN a \n",
"4 NaN NaN z \n",
"\n",
" Notice Preference Definition Provided Email Address \\\n",
"0 phone False \n",
"1 email True \n",
"2 email True \n",
"3 print False \n",
"4 email True \n",
"\n",
" Year Patron Registered Outside of County Supervisor District \\\n",
"0 2003 False 9.0 \n",
"1 2014 False 5.0 \n",
"2 2010 False 4.0 \n",
"3 2016 True NaN \n",
"4 2015 False 3.0 \n",
"\n",
" Membership Duration \n",
"0 163.0 \n",
"1 0.0 \n",
"2 76.0 \n",
"3 0.0 \n",
"4 0.0 "
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.head()"
]
}
],
"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.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}