Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions module1-introduction-to-sql/buddymove_holidayiq.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import sqlite3
import pandas as pd
"""
Load the data (use `pandas`) from the provided file `buddymove_holidayiq.csv`
"""
df = pd.read_csv('buddymove_holidayiq.csv')

"""
- Open a connection to a new (blank) database file `buddymove_holidayiq.sqlite3`
"""
conn = sqlite3.connect('buddymove_holidayiq.sqlite3')
c = conn.cursor()

"""
- Use `df.to_sql`
Load the data (use `pandas`) from the provided file `buddymove_holidayiq.csv`
([documentation](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_sql.html))
to insert the data into a new table `review` in the SQLite3 database
"""
try:
df.to_sql('review', conn)
except ValueError:
print('table already exists')

"""
Then write the following queries (also with `sqlite3`) to test:

- Count how many rows you have - it should be 249!
- How many users who reviewed at least 100 `Nature` in the category also
reviewed at least 100 in the `Shopping` category?
-
"""
querylist = {} # queries

querylist['COUNT_ROWS'] =('SELECT COUNT(*) FROM review')
querylist['DISTINCT_USERS'] = f"SELECT COUNT(DISTINCT(\"User Id\")) FROM review"
cond2 = f"('Nature' >= 100) AND ('Shopping' >= 100)"
querylist['>=100 nature and shopping'] = f"SELECT COUNT('User Id') FROM review WHERE {cond2}"

"""(*Stretch*) What are the average number of reviews for each category?
"""

for q in querylist.items():
name = q[0] #query name
result = c.execute(q[1]).fetchall()[0][0] # query result
print(name,result)

categorys = df.columns.drop(['User Id']).to_list()

for cl in categorys:
result = c.execute(f"SELECT AVG({cl}) FROM review").fetchall()[0][0] # query result
print(cl,result)
"""
Your code (to reproduce all above steps) should be saved in
`buddymove_holidayiq.py`, and added to the repository along with

the generated
SQLite database.

## Resources and Stretch Goals

For a more complicated example SQLite database with a number of tables to play
with, check out this [SQLite Sample
Database](https://www.sqlitetutorial.net/sqlite-sample-database/).

The RPG data also exists in a [JSON
file](https://github.com/LambdaSchool/Django-RPG/blob/master/testdata.json) -
try loading it with the standard [json
module](https://docs.python.org/3.5/library/json.html), and reproducing the
above queries with direct manipulation of the Python dictionaries. Also, try to
load it into a `pandas` dataframe and reproduce the above queries with
appropriate dataframe function calls.
"""
Binary file not shown.
33 changes: 33 additions & 0 deletions module1-introduction-to-sql/json_dict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import json
with open('testdata.json') as json_file:
data = json.load(json_file)

modelkeys= []
for i in data:
if i['model'] not in modelkeys:
modelkeys.append(i['model'])

count_chars = 0
subchar_counts = {} # {'name': count}
armory_item_count = 0
weapon_type_count = 0
for i in data:
name = i['model']
if 'charactercreator'in name: # look at just characters now
if name == 'charactercreator.character': # count the total characters
count_chars += 1
else: # work with character subclasses
if name in subchar_counts:
subchar_counts[name] += 1
else:
subchar_counts[name] = 1
elif 'armory.item' in name:
armory_item_count += 1
elif'armory.weapon' in name:
weapon_type_count += 1

print( f"total character count : {count_chars}\n\n"
f"characters by type : {subchar_counts}\n\n"
f"item type count {armory_item_count}\n"
f"weapon type count {weapon_type_count}\n"
)
Empty file.
269 changes: 269 additions & 0 deletions module1-introduction-to-sql/rpg_json_stratch.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import json \n",
"with open('testdata.json') as json_file:\n",
" data = json.load(json_file)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"- How many total Characters are there?\n",
"- How many of each specific subclass?\n",
"- How many total Items?\n",
"- How many of the Items are weapons? How many are not?\n",
"\n",
"\n",
"- How many Items does each character have? (Return first 20 rows)\n",
"- How many Weapons does each character have? (Return first 20 rows)\n",
"- On average, how many Items does each Character have?\n",
"- On average, how many Weapons does each character have?"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"modelkeys= []\n",
"for i in data:\n",
" if i['model'] not in modelkeys:\n",
" modelkeys.append(i['model'])\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: bson in /home/dliu/anaconda3/lib/python3.7/site-packages (0.5.10)\r\n",
"Requirement already satisfied: python-dateutil>=2.4.0 in /home/dliu/anaconda3/lib/python3.7/site-packages (from bson) (2.8.0)\r\n",
"Requirement already satisfied: six>=1.9.0 in /home/dliu/anaconda3/lib/python3.7/site-packages (from bson) (1.12.0)\r\n"
]
}
],
"source": []
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['charactercreator.character',\n",
" 'charactercreator.fighter',\n",
" 'charactercreator.mage',\n",
" 'charactercreator.cleric',\n",
" 'charactercreator.thief',\n",
" 'charactercreator.necromancer',\n",
" 'armory.item',\n",
" 'armory.weapon']"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"modelkeys\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"total character count : 302\n",
"\n",
"characters by type : {'charactercreator.fighter': 68, 'charactercreator.mage': 108, 'charactercreator.cleric': 75, 'charactercreator.thief': 51, 'charactercreator.necromancer': 11}\n",
"\n",
"item type count 174\n",
"weapon type count 37\n",
"\n"
]
}
],
"source": [
"count_chars = 0\n",
"subchar_counts = {} # {'name': count}\n",
"armory_item_count = 0 \n",
"weapon_type_count = 0\n",
"for i in data:\n",
" name = i['model']\n",
" if 'charactercreator'in name: # look at just characters now\n",
" if name == 'charactercreator.character': # count the total characters \n",
" count_chars += 1 \n",
" else: # work with character subclasses\n",
" if name in subchar_counts:\n",
" subchar_counts[name] += 1\n",
" else: \n",
" subchar_counts[name] = 1\n",
" elif 'armory.item' in name:\n",
" armory_item_count += 1\n",
" elif'armory.weapon' in name:\n",
" weapon_type_count += 1\n",
" \n",
"print( f\"total character count : {count_chars}\\n\\n\" \n",
" f\"characters by type : {subchar_counts}\\n\\n\"\n",
" f\"item type count {armory_item_count}\\n\" \n",
" f\"weapon type count {weapon_type_count}\\n\" \n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['ABCMeta',\n",
" 'BSONCoding',\n",
" 'Decimal',\n",
" 'ELEMENT_TYPES',\n",
" 'MissingClassDefinition',\n",
" 'MissingTimezoneWarning',\n",
" 'ObjectId',\n",
" 'PY3',\n",
" 'StringIO',\n",
" 'TraversalStep',\n",
" 'UUID',\n",
" 'UnknownSerializerError',\n",
" '__all__',\n",
" '__builtins__',\n",
" '__cached__',\n",
" '__doc__',\n",
" '__file__',\n",
" '__loader__',\n",
" '__name__',\n",
" '__package__',\n",
" '__path__',\n",
" '__spec__',\n",
" 'abstractmethod',\n",
" 'b2a_hex',\n",
" 'calendar',\n",
" 'classes',\n",
" 'codec',\n",
" 'datetime',\n",
" 'decode_binary_subtype',\n",
" 'decode_document',\n",
" 'decode_object',\n",
" 'dumps',\n",
" 'encode_array',\n",
" 'encode_array_element',\n",
" 'encode_binary',\n",
" 'encode_binary_element',\n",
" 'encode_boolean_element',\n",
" 'encode_cstring',\n",
" 'encode_document',\n",
" 'encode_document_element',\n",
" 'encode_double',\n",
" 'encode_double_element',\n",
" 'encode_int32_element',\n",
" 'encode_int64_element',\n",
" 'encode_none_element',\n",
" 'encode_object',\n",
" 'encode_object_element',\n",
" 'encode_object_id_element',\n",
" 'encode_string',\n",
" 'encode_string_element',\n",
" 'encode_uint64_element',\n",
" 'encode_utc_datetime_element',\n",
" 'encode_value',\n",
" 'import_class',\n",
" 'import_classes',\n",
" 'import_classes_from_modules',\n",
" 'integer_types',\n",
" 'iterkeys',\n",
" 'loads',\n",
" 'objectid',\n",
" 'patch_socket',\n",
" 'py3compat',\n",
" 'struct',\n",
" 'text_type',\n",
" 'tz_util',\n",
" 'tzutc',\n",
" 'utc',\n",
" 'warnings',\n",
" 'xrange']"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": []
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"import bson"
]
}
],
"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
}
Loading