Тёмный

Run SQL in Jupyter Notebooks - Python Recipes 

TekMinded - Python Recipes
Подписаться 945
Просмотров 63 тыс.
50% 1

Learn how to use Jupyter Notebooks to read and right data from Databases with SQL.
The procedures shown on this video work with any database that has a python API and can serve as a quick and practical way to retrieve and/or report data for analysis.
#jupiter #jupyter #sql #python
Table of Contents:
00:32 - Why would I use SQL in Jupyter Notebooks?
01:00 - Subscribe and Like
01:25 - Importing rquired libraries
01:55 - Install iPython-sql library
02:10 - Creating Demo Data
03:27 - Connect to the db and run SQL
SUBSCRIBE TO OUR RU-vid CHANNEL
/ @tekminded
################################
CODE
################################
import libraries
import pandas as pd
import sqlite3
install the ipython-sql libray
!pip install ipython-sql
create a dataframe to import data for the tutorial
df= pd.DataFrame({'name': ['Juan', 'Victoria', 'Mary'],
'age': [23,34,43],
'city': ['Miami', 'Buenos Aires','Santiago']})
df
We will sqlite3 library and create a connection
cnn = sqlite3.connect('jupyter_sql_tutorial.db')
df.to_sql('people', cnn)
%load_ext sql
%sql sqlite:///jupyter_sql_tutorial.db
%%sql
SELECT *
FROM people
%%sql
SELECT count(*)
FROM people
%%sql
SELECT sum(age) as 'age_sum'
FROM people
################################
/ CODE
################################

Опубликовано:

 

10 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 53   
@mulhaswansaputra6602
@mulhaswansaputra6602 2 года назад
Thank you so much for your video, I was have problem using sql in my jupyter notebook and many tutorial I had follow and it result always error or anything else. but with your video I can do it now. thank you
@bragedybwad4580
@bragedybwad4580 3 года назад
THANK YOU SO MUCH! Short, straight forward, easy to understand. Helped me a lot
@TekMinded
@TekMinded 3 года назад
Great! Thanks for the feedback!!
@denisepatrick5547
@denisepatrick5547 2 года назад
Very straightforward and works on my end. Thank you!
@user-bq8uy5rq1d
@user-bq8uy5rq1d 6 дней назад
Спасибо большое ❤ Thank you very much
@Ashrafsakr-w8d
@Ashrafsakr-w8d Год назад
thank u so much it help me more , our profit Mohamed teach us He who does not thank people does not thank God
@sahanav2678
@sahanav2678 3 месяца назад
Thanks for sharing. It was very helpful
@SriRam-yq4id
@SriRam-yq4id 2 года назад
Thank you so much for a short, yet very useful information.
@gangulaveerareddy
@gangulaveerareddy Год назад
very nice Explanation I have a doubt i loaded a csv file into table into sqlite3 using pd.read_csv. when i access that table from sqlite3 to pandas df using pd.read_sql_table() it is showing NotImplementedError kindly help to reslove this.
@zubee6011
@zubee6011 2 года назад
I keep getting 'The SQL module is not an ipython extension' when I run the %load_ext sql command. As for the database, I'm using postgreSQL. After connecting it I got a UsageError: Line magic function '%sql' not found
@nabanitadasgupta
@nabanitadasgupta Год назад
Thank you so much. It was really helpful.
@patite3103
@patite3103 3 года назад
Great video! Could you please make another video explaining how to connect to a file saved in a given directory and to a file in github? Thank you
@fabiogustavo8832
@fabiogustavo8832 9 месяцев назад
Perfect ! you helped me a lot with this nice video :D
@pulokchakraborty9569
@pulokchakraborty9569 2 года назад
Awesome video, really good. Thank you so much.
@TekMinded
@TekMinded Год назад
Glad you liked it!
@RyanLBuchanan
@RyanLBuchanan 3 года назад
Fabulous! Thanks a bunch. You are awesome!
@TekMinded
@TekMinded 3 года назад
You are welcome
@josearmandovivero408
@josearmandovivero408 Год назад
Thanks! Very well explained!
@anushkamote2468
@anushkamote2468 6 месяцев назад
how to run sql query for 2 tables with this code?
@ssgoh4968
@ssgoh4968 3 года назад
Thanks. Didn't know we can do stuff like that.
@TekMinded
@TekMinded 3 года назад
Happy you liked!!
@priteekadam9584
@priteekadam9584 3 года назад
thnx a lot it solved my prblm
@TekMinded
@TekMinded 3 года назад
Most welcome
@jamesleleji6984
@jamesleleji6984 3 года назад
I have MySQL workbench installed on my machine. How do i connect it to jupyter notebook? Thanks
@TekMinded
@TekMinded 3 года назад
Hi James - Adapt the following to your code when creating the connection: conn=pymysql.connect(host='localhost',port=int(3306),user='root',passwd='YOUR_PASSWORD',db='YOUR_DATABASENAME') Or course, please make sure to change the user, password e db name to the ones you are using. if the host is also not in your local machine you will need to point to the right host IP. It should do the job for you!!.. Let me know.
@margotpaon
@margotpaon 5 месяцев назад
Helpful tks!
@rimshavirmani4520
@rimshavirmani4520 3 года назад
Thank you so much! Great tutorial
@TekMinded
@TekMinded 3 года назад
Glad you enjoyed it!
@rabhishekshetty7003
@rabhishekshetty7003 2 года назад
Is it possible to read .sql file in jupyter notebook ?
@ericGTS
@ericGTS 2 года назад
Thanks for sharing!!
@TekMinded
@TekMinded 2 года назад
My pleasure!!
@nth.education
@nth.education 2 года назад
Well explained, is there a way to connect to postgresql as well ?
@patite3103
@patite3103 3 года назад
Thank you for the video! Would it be possible to do a video showing the adaptation to Jupyterlab? Thank you!
@TekMinded
@TekMinded 3 года назад
hi @patite there should not really be any adaptation for JupyterLab as JupyterLab run pretty much almost the same notebook as Jupyter and with equivalent functionality. SQLlite is also part of the python installation so you should not find differences either... give it a try and let me know if you find it different.
@patite3103
@patite3103 3 года назад
@@TekMinded I get an error for the line "SELECT *". Could you please run it in Jupyterlab?
@TekMinded
@TekMinded 3 года назад
​@@patite3103 it works with no errors in Jupyterlab as well. There must be an issue with your installation or configuration. Sorry you are experiencing problems.
@patite3103
@patite3103 2 года назад
@@TekMinded which Python version do you use?Do you have to install sqlite3?
@jamesleleji6984
@jamesleleji6984 3 года назад
How do you connect using MySQL instead of sqlite3? Thanks
@TekMinded
@TekMinded 3 года назад
Adapt the following to your code when creating the connection: conn=pymysql.connect(host='localhost',port=int(3306),user='root',passwd='YOUR_PASSWORD',db='YOUR_DATABASENAME') Or course, please make sure to change the user, password e db name to the ones you are using. if the host is also not in your local machine you will need to point to the right host IP. Hope it helps...
@mohamedlajmi5820
@mohamedlajmi5820 2 года назад
what's is the type of df plz ?
@bharatbhanushali
@bharatbhanushali 2 года назад
Thank you. How create a dataframe from the output of the select table?
@TekMinded
@TekMinded 2 года назад
Well, you dump the output of your query with a create table statement but honestly if you want to work with data frames, you are better off just access the database directly. If you want to use SQL because you are not too confident with data manipulation in python this video can help you. ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-qUNwjDtZmTs.html. good luck!!
@narayanakarumuri6049
@narayanakarumuri6049 3 года назад
Can we run it without MySQL in our system??
@TekMinded
@TekMinded 3 года назад
yes. absolutely. this example uses mysqlite that comes pre-installed with most python installations. It creates a mysqllite file that you can even directly use in other machines.
@cesararnaez4665
@cesararnaez4665 3 года назад
Thanks!!!!
@TekMinded
@TekMinded 3 года назад
You are welcome
@jamesleleji6984
@jamesleleji6984 3 года назад
Do we need to install mysqlite on our system first?
@TekMinded
@TekMinded 3 года назад
No. Python already comes with it.
@jamesleleji6984
@jamesleleji6984 3 года назад
How do you create a connection with MySQL into jupyter notebook and then load your dataset?. Thank you
@TekMinded
@TekMinded 3 года назад
Adapt the following to your code when creating the connection: conn=pymysql.connect(host='localhost',port=int(3306),user='root',passwd='YOUR_PASSWORD',db='YOUR_DATABASENAME') Or course, please make sure to change the user, password e db name to the ones you are using. if the host is also not in your local machine you will need to point to the right host IP.
@emersonhs1
@emersonhs1 2 года назад
awesome thanks
@TekMinded
@TekMinded Год назад
Our pleasure!
@prth5636
@prth5636 2 года назад
How to create table
Далее
How to Create Pivot Tables with Python?
8:07
Просмотров 1,6 тыс.
Introducing iPhone 16 | Apple
02:00
Просмотров 4,5 млн
SQL Databases with Pandas and Python - A Complete Guide
16:59
Why I stopped using Jupyter Notebooks
11:17
Просмотров 14 тыс.
Learn SQL In 60 Minutes
56:24
Просмотров 2,1 млн
How to Use SQL with Excel using Python
16:52
Просмотров 122 тыс.
Magic commands in Jupyter and IPython
7:33
Просмотров 4,3 тыс.
Connecting PostgreSQL with Jupyter Notebook
24:57
Просмотров 4,3 тыс.