Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for IMPORTING DATA USING PYTHON TO MYSQL
allan-pg
allan-pg

Posted on

     

IMPORTING DATA USING PYTHON TO MYSQL

Introduction

Importing data manually into your database especially when its a number of tables, can not only be tiresome but also time consuming. This can be made easier by use of python libraries.

Download painting dataset fromkaggle. Paintings data set is made up of 8 csv files that we will import to our database by use of a simple python script, instead of importing data to our database table manually.

Steps to import data

  • Create database in PG-admin and call it painting
createdatabasepainting
Enter fullscreen modeExit fullscreen mode
  • Open jupyter notebook and install python libraries
pipinstallsqlalchemypipinstallpandas
Enter fullscreen modeExit fullscreen mode
  • Import Python libraries
importpandasaspdfromsqlalchemyimportcreate_engine
Enter fullscreen modeExit fullscreen mode
  • Create a connection to your pg-admin database
conn_string='postgresql://postgres:1344@localhost/painting'db=create_engine(conn_string)conn=db.connect()
Enter fullscreen modeExit fullscreen mode

In conn_string stores url to our database where postgresql is our database since we created database in pg-admin, postgres is default name of our database name pg-admin, 1344 is password to our pg-admin,@localhost is host name and painting is our database name

  • Load files to your database
files=['artist','canvas_size','image_link','museum','museum_hours','product_size','subject','work']forfileinfiles:df=pd.read_csv(fr"C:\Users\Admin\Desktop\famous painti\{file}.csv")df.to_sql(file,con=conn,if_exists='replace',index=False)
Enter fullscreen modeExit fullscreen mode

files is the names we wish to name our tables in painting database. for loop is used so that we can iterate over our files. fr in pd.read csv represents f string and raw data. Index must be set to false to not import default pandas index.

Conclusion

We have learned how to use Python and MySQL Connector to create an entirely new database in MySQL Server, create tables within that database instead of doing it manually.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Tech enthusiastic
  • Education
    BSC Mathematics and Computer Science
  • Work
    Data Analyst
  • Joined

More fromallan-pg

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp