Skip to content

Commit e377b8a

Browse files
authored
Merge pull request #255 from jim-dotcom/new-commands
SQL-332: Run SQL query on CSV file contents from command-line
2 parents 50db492 + 542f109 commit e377b8a

11 files changed

+54
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name,sex,age,weight,height
2+
Alex,M,41,74,170
3+
Bert,M,42,66,166
4+
Dave,M,39,72,167
5+
Elly,F,30,70,124
6+
Luke,M,34,72,163
7+
Omar,M,38,69,145
8+
Page,F,31,67,135
9+
Ruth,F,28,65,131
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import duckdb
2+
3+
# Run a query directly on the CSV file
4+
query = "SELECT AVG(weight) AS avg_weight FROM read_csv_auto('data.csv')"
5+
avg_weight = duckdb.query(query).to_df()
6+
print(avg_weight)
7+
8+
# Another query to count individuals by gender
9+
count_gender_query = "SELECT sex, COUNT(*) AS count FROM read_csv_auto('data.csv') GROUP BY sex"
10+
count_gender = duckdb.query(count_gender_query).to_df()
11+
print(count_gender)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import pandas as pd
2+
from pandasql import sqldf
3+
4+
# Load the CSV file into a DataFrame
5+
df = pd.read_csv('data.csv')
6+
7+
# Define the query function
8+
pysqldf = lambda q: sqldf(q, globals())
9+
10+
# Example 1: Getting average weight
11+
avg_weight_query = "SELECT AVG(weight) AS avg_weight FROM df"
12+
avg_weight = pysqldf(avg_weight_query)
13+
print(avg_weight)
14+
15+
16+
# Example 2: Count individuals by gender
17+
count_gender_query = "SELECT sex, COUNT(*) AS count FROM df GROUP BY sex"
18+
count_gender = pysqldf(count_gender_query)
19+
print(count_gender)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sqlite3 :memory: -cmd '.mode csv' -cmd '.import data.csv my_table'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pip install duckdb
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pip install pandas pandasql sqlalchemy==1.4.46
2+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Intall SQLite
2+
sudo apt-get install sqlite3
3+
4+
# Check version of SQLite
5+
sqlite3 --version
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sqlite3 :memory: -cmd '.mode csv' -cmd '.import data.csv my_table' 'SELECT sex, COUNT(*) FROM my_table GROUP BY sex;'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python example2_csv.py
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python example_csv.py

0 commit comments

Comments
 (0)