forked from chattermill/data-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreddit_data_SQL_queries.py
More file actions
46 lines (29 loc) · 1.22 KB
/
reddit_data_SQL_queries.py
File metadata and controls
46 lines (29 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import sqlite3
import pandas as pd
commands = {
"avg_score_by_iso_grouping": '''SELECT iso, AVG(score)
FROM reviews
GROUP BY iso''',
"max_score_by_app_bought_grouping": '''SELECT apps_bought_bucket, MAX(score)
FROM reviews
GROUP BY apps_bought_bucket''',
"avg_score_over_time_grouping": '''SELECT date, AVG(score)
FROM reviews
GROUP BY date'''
}
def sql_query(command, database_path):
try:
database = sqlite3.connect(database_path)
cursor = database.cursor()
cursor.execute(command)
queried_data = cursor.fetchall()
database.close()
return queried_data
except Exception as e:
print(e)
def write_all_to_csv(database_path):
for key, value in commands.items():
query_result = sql_query(value, database_path)
dataframe = pd.DataFrame(query_result)
dataframe.to_csv(key + ".csv")
write_all_to_csv("exercise_database.db")