Srihari Thyagarajan commited on
Commit
3fb7b66
·
unverified ·
2 Parent(s): b6d86d5 27a6ebd

Merge pull request #94 from marimo-team/Mustjaab/main

Browse files
Files changed (1) hide show
  1. duckdb/DuckDB_Loading_CSVs.py +173 -0
duckdb/DuckDB_Loading_CSVs.py ADDED
@@ -0,0 +1,173 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # /// script
2
+ # requires-python = ">=3.10"
3
+ # dependencies = [
4
+ # "marimo",
5
+ # "plotly.express",
6
+ # "plotly==6.0.1",
7
+ # "duckdb==1.2.1",
8
+ # "sqlglot==26.11.1",
9
+ # "pyarrow==19.0.1",
10
+ # "polars==1.27.1",
11
+ # ]
12
+ # ///
13
+
14
+ import marimo
15
+
16
+ __generated_with = "0.12.10"
17
+ app = marimo.App(width="medium")
18
+
19
+
20
+ @app.cell(hide_code=True)
21
+ def _(mo):
22
+ mo.md(r"""#Loading CSVs with DuckDB""")
23
+ return
24
+
25
+
26
+ @app.cell(hide_code=True)
27
+ def _(mo):
28
+ mo.md(
29
+ r"""
30
+ <p> I remember when I first learnt about DuckDB, it was a gamechanger — I used to load the data I wanted to work on to a database software like MS SQL Server, and then build a bridge to an IDE with the language I wanted to use like Python, or R; it was quite the hassle. DuckDB changed my whole world — now I could just import the data file into the IDE, or notebook, make a duckdb connection, and there we go! But then, I realized I didn't even need the step of first importing the file using python. I could just query the csv file directly using SQL through a DuckDB connection.</p>
31
+
32
+ ##Introduction
33
+ <p> I found this dataset on the evolution of AI research by discipline from <a href= "https://oecd.ai/en/data?selectedArea=ai-research&selectedVisualization=16731"> OECD</a>, and it piqued my interest. I feel like publications in natural language processing drastically jumped in the mid 2010s, and I'm excited to find out if that's the case. </p>
34
+
35
+ <p> In this notebook, we'll: </p>
36
+ <ul>
37
+ <li> Import the CSV file into the notebook</li>
38
+ <li> Create another table within the database based on the CSV</li>
39
+ <li> Dig into publications on natural language processing have evolved over the years</li>
40
+ </ul>
41
+ """
42
+ )
43
+ return
44
+
45
+
46
+ @app.cell(hide_code=True)
47
+ def _(mo):
48
+ mo.md(r"""##Load the CSV""")
49
+ return
50
+
51
+
52
+ @app.cell
53
+ def _(mo):
54
+ _df = mo.sql(
55
+ f"""
56
+ /* Another way to load the CSV could be
57
+ SELECT *
58
+ FROM read_csv('https://github.com/Mustjaab/Loading_CSVs_in_DuckDB/blob/main/AI_Research_Data.csv')
59
+ */
60
+ SELECT *
61
+ FROM "https://raw.githubusercontent.com/Mustjaab/Loading_CSVs_in_DuckDB/refs/heads/main/AI_Research_Data.csv"
62
+ LIMIT 5;
63
+ """
64
+ )
65
+ return
66
+
67
+
68
+ @app.cell(hide_code=True)
69
+ def _(mo):
70
+ mo.md(r"""##Create Another Table""")
71
+ return
72
+
73
+
74
+ @app.cell
75
+ def _(mo):
76
+ Discipline_Analysis = mo.sql(
77
+ f"""
78
+ -- Build a table based on the CSV where it just contains the specified columns
79
+ CREATE TABLE Domain_Analysis AS
80
+ SELECT Year, Concept, publications FROM "https://raw.githubusercontent.com/Mustjaab/Loading_CSVs_in_DuckDB/refs/heads/main/AI_Research_Data.csv"
81
+ """
82
+ )
83
+ return Discipline_Analysis, Domain_Analysis
84
+
85
+
86
+ @app.cell
87
+ def _(Domain_Analysis, mo):
88
+ Analysis = mo.sql(
89
+ f"""
90
+ SELECT *
91
+ FROM Domain_Analysis
92
+ GROUP BY Concept, Year, publications
93
+ ORDER BY Year
94
+ """
95
+ )
96
+ return (Analysis,)
97
+
98
+
99
+ @app.cell
100
+ def _(Domain_Analysis, mo):
101
+ _df = mo.sql(
102
+ f"""
103
+ SELECT
104
+ AVG(CASE WHEN Year < 2020 THEN publications END) AS avg_pre_2020,
105
+ AVG(CASE WHEN Year >= 2020 THEN publications END) AS avg_2020_onward
106
+ FROM Domain_Analysis
107
+ WHERE Concept = 'Natural language processing';
108
+ """
109
+ )
110
+ return
111
+
112
+
113
+ @app.cell
114
+ def _(Domain_Analysis, mo):
115
+ NLP_Analysis = mo.sql(
116
+ f"""
117
+ SELECT
118
+ publications,
119
+ CASE
120
+ WHEN Year < 2020 THEN 'Pre-2020'
121
+ ELSE '2020-onward'
122
+ END AS period
123
+ FROM Domain_Analysis
124
+ WHERE Year >= 2000
125
+ AND Concept = 'Natural language processing';
126
+ """,
127
+ output=False
128
+ )
129
+ return (NLP_Analysis,)
130
+
131
+
132
+ @app.cell
133
+ def _(NLP_Analysis, px):
134
+ px.box(NLP_Analysis, x='period', y='publications', color='period')
135
+ return
136
+
137
+
138
+ @app.cell(hide_code=True)
139
+ def _(mo):
140
+ mo.md(r"""<p> We can see there's a significant increase in NLP publications 2020 and onwards which definitely makes sense provided the rapid emergence of commercial large language models, and AI assistants. </p>""")
141
+
142
+ @app.cell(hide_code=True)
143
+ def _(mo):
144
+ mo.md(
145
+ r"""
146
+ ##Conclusion
147
+ <p> In this notebook, we learned how to:</p>
148
+ <ul>
149
+ <li> Load a CSV into DuckDB </li>
150
+ <li> Create other tables using the imported CSV </li>
151
+ <li> Seamlessly analyze and visualize data between SQL, and Python cells</li>
152
+ </ul>
153
+ """
154
+ )
155
+ return
156
+
157
+
158
+ @app.cell
159
+ def _():
160
+ import pyarrow
161
+ import polars
162
+ return polars, pyarrow
163
+
164
+
165
+ @app.cell
166
+ def _():
167
+ import marimo as mo
168
+ import plotly.express as px
169
+ return mo, px
170
+
171
+
172
+ if __name__ == "__main__":
173
+ app.run()