Pandas Dataframes and SQL [How to write dataframes into a sql database/get sql table to dataframe]

Поділитися
Вставка
  • Опубліковано 14 чер 2024
  • In this video I am showing how to get sql data into Pandas dataframes and also how to store dataframes into sql databases. I am working with SQLite in this video.
    Get the Notebook/Source code by becoming a Tier-2 Channel member:
    / @algovibes
    More information for other Database systems (e.g. MySQL or MS SQL server or Postgres):
    docs.sqlalchemy.org/en/14/cor...
    Check out my introductory video on Python and SQL:
    • How to connect Python ...
    Please be so kind subscribing and liking the video in case it was helpful.
    0:00 - 0:55 Intro
    0:55 - 02:08 Create DB engine (read description for other DB)
    02:08 - 03:49 read_sql (Both whole table and SQL syntax possible)
    03:49 - 08:32 to_sql (and possible errors/mistakes/pains)
    08:32 - 10:20 Pull and store financial data in a sql db
    10:20 - 11:04 Outro / Potential topics
    #Pandas #SQL #Python

КОМЕНТАРІ • 82

  • @jessedayan33
    @jessedayan33 3 місяці тому +1

    Thank you! Looking forward to working through all of your videos!

    • @Algovibes
      @Algovibes  3 місяці тому +1

      Thanks Jesse! Let me know if you need something and I keep my fingers crossed for you! Cheers Algovibes

  • @ihebbibani7122
    @ihebbibani7122 3 роки тому +2

    I really really appreciate your videos. I love you accent , how you explain , your method ..everything is great. Already , subscribed and I am sharing your videos ;)

    • @Algovibes
      @Algovibes  3 роки тому

      Thank you buddy, really appreciate your kind words and your support :-)

  • @xilllllix
    @xilllllix 2 роки тому +1

    EXACTLY what i was looking for, thank you!

    • @Algovibes
      @Algovibes  2 роки тому

      Awesome. Thanks a lot for watching.

  • @davidtillery6250
    @davidtillery6250 2 роки тому +1

    Thank you! your videos have been a big help to get my Tranding Bot jurney started!

    • @Algovibes
      @Algovibes  2 роки тому

      Thanks a lot for your feedback man. That's awesome to read!

  • @scrambled_egggs
    @scrambled_egggs 2 роки тому +2

    Thank you. You gave me courage to challenge my project!

    • @Algovibes
      @Algovibes  2 роки тому

      That's awesome man. Thanks a lot for using the Super Thanks feature and greetings to South Korea!

  • @jitkadyan
    @jitkadyan 3 роки тому +1

    Another informative video sir , thanks and also very thankful to you replying our comments and providing information as suggested by us by spending your precious time to create such video , we are looking for more video , thanks again sir.

    • @Algovibes
      @Algovibes  3 роки тому

      Thanks a lot for your support :-) Really appreciate it.
      Main objective of this channel is to create a community where we can exchange about programming, finance and Data Science so I am doing my best to keep the comment section alive :D

  • @hicarlee8762
    @hicarlee8762 2 роки тому +1

    gr8 upload and understandings for pandas!!

    • @Algovibes
      @Algovibes  2 роки тому

      Awesome mate, thanks for your feedback :-)

  • @randomdude79404
    @randomdude79404 3 роки тому +1

    Great video brother very useful

    • @Algovibes
      @Algovibes  3 роки тому

      Thanks for watching mate :-) Appreciate your comment.

  • @AbCdEf-vh7jj
    @AbCdEf-vh7jj 2 роки тому +6

    I can't tell you how enjoyable it is to listen and practice to your vids! You're doing a fantastic job and enrich every participant with the huge amount of learnings you offer us enthusiasts :)
    I will use your learnings for my own db (also retrieved from yfinance). 2 questions occured, which I would like to ask you (your answers would be very much appreciated):
    (1) When combining a database of Japanese or Chinese stocks with American or European stocks, the trading days are different. How do you personally go for database calculations like the 3M performance with stocks with diverging dates (used as indices)?
    (2) Would it be possible to say sth like: I would like to store the price data from 2000-01-01 to 2021-12-01 and then only append the price data of that very day? Through the help of your video, it seems doable. What's your opinion on that?
    Thank you so much for your great community contribution, Algo! :-)

    • @Algovibes
      @Algovibes  2 роки тому

      Hey man, thank you so much for your kind words. That's truly awesome to read.
      Regarding your first question:
      Can't you do the calculation in kind of different "buckets"? So create a function pulling data and implement a conditional calculation?
      Regarding the second one: Yes you can do that. I was covering daily updates of an example DataBase in my Stock recommendation playlist.

  • @zaheerahmed253
    @zaheerahmed253 3 місяці тому +1

    Thanks really helpful

    • @Algovibes
      @Algovibes  2 місяці тому

      Happy to read, thx mate!

  • @sz8558
    @sz8558 2 роки тому +2

    Another great video..thank you! In case anyone gets stuck at 7:17..appending df1 with error message reading "database is locked"...just restart your PC and try again. Seems to fix the issue.

    • @Algovibes
      @Algovibes  2 роки тому +1

      Yes that's an annoying error using sqlite. Thanks for sharing a possible solution! To avoid that in general as an additional solution just be sure you are not currently querying or are using the DB with another script. OR just use MySQL :-)

    • @azamat_kalam
      @azamat_kalam 2 роки тому

      Works like butter.
      Everyone needs a fresh start from time to time, even our machines

    • @kennithsmit2356
      @kennithsmit2356 Рік тому

      ​@@Algovibes I had the same error watching your videos back to back.
      It took me some time to figure out i have to run connection.close() on previous notepad.
      Thanks for the great work

  • @alibulus4382
    @alibulus4382 Рік тому +1

    You are the best

  • @oczko1986
    @oczko1986 2 роки тому +1

    i did the "replace mistake" today! :D

  • @CK.23.
    @CK.23. 3 роки тому +1

    Grate job. Go on

    • @Algovibes
      @Algovibes  3 роки тому

      Thanks mate. I will do my best :-)

  • @vazquezlunadiegojose8974
    @vazquezlunadiegojose8974 2 роки тому +1

    Thanks,your videos are great
    What if want to add primary or foreign keys to the columns ?

    • @Algovibes
      @Algovibes  2 роки тому

      Hi buddy, thanks a lot for you kind comment :-)
      Is this covering your question?
      stackoverflow.com/questions/38085624/pandas-create-foreign-id-column-based-on-name-column

  • @Kig_Ama
    @Kig_Ama Рік тому +1

    9:14 How would u download the fundamental data of GME into the db?

  • @brayan.zapata.
    @brayan.zapata. 2 роки тому +1

    thanks !

    • @Algovibes
      @Algovibes  2 роки тому

      Thanks for watching :-)

  • @chinmoybasak7749
    @chinmoybasak7749 2 роки тому +1

    Thank you! for sharing! But How can we establish a relationship(keys) in between the imported tables? Would really appreciate it!

    • @Algovibes
      @Algovibes  2 роки тому

      Welcome! This is something you would do with SQL and not pandas (well you can execute SQL syntax with pandas tho) if I got your question right (adding e.g. primary keys).

  • @styvkygo
    @styvkygo 3 роки тому +1

    You are providing very helpful content; I have been able to fix some issues in my codes thanks to you. I really appreciate what you are doing.
    I have a question regarding resampling data if you do not mind assisting me on this please. what would be the best way to resample downloaded stock data from say 1m to 5m or 15m? Cheers

    • @Algovibes
      @Algovibes  3 роки тому +1

      Thanks a lot for watching and your kind comment :-)
      Apply this to your downloaded (1 minute) dataframe:
      df.resample('5min').first()
      Be kindly invited to provide me feedback if this solves your problem.
      Best regards

    • @styvkygo
      @styvkygo 3 роки тому +1

      @@Algovibes Hi, the code works. Thank you.
      Another question if you don't mind, in case I would like to download multiple assets at once and save all of them to individually, to the same engine, how would you best run this.
      for instance:
      asset = 'AAPL', 'FB', 'TSLA'
      Also when I run this code, all the assets appear all on the same rows & columns. can you view them separately
      df = yf.download(asset,start='2021-04-06',end='2021-04-07',interval='1m')
      df
      How would you save all the assets in their respective engines?
      df.to_sql('_______',engine)

    • @Algovibes
      @Algovibes  3 роки тому +1

      Check out the most recent video on the channel. I am showing how I would do that there.
      Hope this is solving your problem!

    • @styvkygo
      @styvkygo 3 роки тому +1

      @@Algovibes Thank you very much for the new video, I have been trying to figure how to loop for the last 3 days but the way you doing is just awesome. I just finished working on the new video.
      I like the way you write your codes and the thought process, do you have any recommendations on good materials eg books, links, etc? Your content is much better and quicker to grasp than what my lecturers offers

    • @Algovibes
      @Algovibes  3 роки тому +1

      I honestly learned Python the hard way. I had some background in programming before but I started out with an university course and besides that I used some DataCamp. I was becoming proficient in Python by writing my masterthesis in it. The best way to learn is writing programs in it. I can only tell you it is a process which is taking time :-D
      For the very basics I think we used Automate the boring stuff and Head first Python in university. I can personally recommend Sentdex as a role model of mine.
      Hope this is helpful for you!

  • @srikanth0venkataswam
    @srikanth0venkataswam 2 роки тому +1

    Thanks

    • @Algovibes
      @Algovibes  2 роки тому

      Thanks for watching :-)

  • @mrunalcavale2270
    @mrunalcavale2270 2 роки тому +1

    Hi, this was really helpful. I also wanted to know how to upsert data into sql server. Can you share some info about that? My requirement is, the table cannot be truncated at any point and therefore I cannot do truncate and load. I need to either update or insert the records into DB. Your suggestion is deeply appreciated.

    • @Algovibes
      @Algovibes  2 роки тому +1

      Happy that it was helpful! :-) Thanks for watching and your interesting question regarding performing an upsert query.
      Ad hoc I cannot answer it but this could help you out:
      github.com/ryanbaumann/Pandas-to_sql-upsert

    • @mrunalcavale2270
      @mrunalcavale2270 2 роки тому +1

      @@Algovibes Thank you! I will check the code. Appreciate your help!

  • @cvillejin
    @cvillejin 2 роки тому +1

    Nice video and tutoria!. Do you know how I can do this with SQL Server? I don't really know what SQLlite and all this technical; stuff is, but I tried creating this on Spyder and I can't re-create the same results. I use SQL Server and SSMS. Your help would be greatly appreciated!
    Edit: specifically, I am trying to recreate the part where you use yfinance to import the live ticker data into a SQL Server database that is made in SSMS

    • @Algovibes
      @Algovibes  2 роки тому

      Hi mate, thanks a lot for watching and your comment. Sure, you can, I know this because I am also working with MSSQL.
      Defining the engine is slightly different for MSSQL:
      docs.sqlalchemy.org/en/14/core/engines.html

  • @felixbartschi6830
    @felixbartschi6830 Рік тому +1

    Thanks for the nice tutorial.
    Is there a way to update rows without defining a SQL schema?
    something like: df.to_sql('table', engine, if_exists='update)?
    I do have a .csv files with +800 columns writing sql update statement is a pain i have seen the ubsert solution but it includes SQL as well.

    • @Algovibes
      @Algovibes  Рік тому

      Hi Felix,
      yes, that's possible!
      I have shown it in e.g. this video:
      ua-cam.com/video/TJK-Ct9WnCw/v-deo.html

  • @darckmate
    @darckmate 2 роки тому +1

    is this notebook available?

  • @guilhermerezende3359
    @guilhermerezende3359 2 роки тому +1

    in matters of time and processing, is that way batter than using pyodbc?

    • @Algovibes
      @Algovibes  2 роки тому +1

      Not really comparable imo. I am using a sqlite DB with sqlalchemy. As far as I know sqlite isn't compatible with pyodbc.

  • @nguyenduyta7136
    @nguyenduyta7136 3 роки тому +1

    Thank so much. By the way I would like to creat a GUI that user can sign up and log in and have profiles of users to database with portfolio management. Please tell me if you have this course

    • @Algovibes
      @Algovibes  3 роки тому +1

      Thank you for watching and your comment :-) May you please clarify what you mean with if I have this course? I don't get it :-D
      Nevertheless he project sounds pretty nice! Would love to see it when you finalized it.

    • @nguyenduyta7136
      @nguyenduyta7136 3 роки тому +1

      @@Algovibes Hi , I mean if you create a course that teach me how to solve my wish

  • @ugurpotaker8903
    @ugurpotaker8903 2 роки тому +1

    I am getting "database is locked" error when I type "df1.to_sql('employees', engine, if_exists = 'append', index = False)". Any solution would be appreciated.

    • @Algovibes
      @Algovibes  2 роки тому

      Delete the database and set it up again should do the job.

    • @neu_1
      @neu_1 2 роки тому

      @@Algovibes Hi, getting the same issue too. I deleted and reset the database, the the error message saying database is locked once again. May I know any alternative solution? Appreciate the answer and help

  • @lilymaths4584
    @lilymaths4584 3 роки тому +1

    Hello there, thank you for uploading! I am struggling with loading my pandas data frame with millions of rows into my Postgres database and I keep having issues! Would you be able to help me?

    • @Algovibes
      @Algovibes  3 роки тому +1

      Hi and thank you very much for watching :-) In that case it is necessary to define a chunk size. Did you already give that a shot?

    • @lilymaths4584
      @lilymaths4584 3 роки тому +1

      @@Algovibes yes I did! Nothing seems to load in the table but my csv file headers do load!

    • @Algovibes
      @Algovibes  3 роки тому +1

      @@lilymaths4584 I need more information. You have a dataframe consisting of million rows right? And you are using the df.to_sql Syntax and define a chunksize like this: df.to_sql(„tablename“, engine, chunksize = 10000) ?
      Did you already try to just write in a single row to see if that works out?

    • @lilymaths4584
      @lilymaths4584 3 роки тому +1

      @@Algovibes hello algovibes, thank you for your help so much! My colleague helped me load the data in. It turns out the error was with an extra column being read in so we had to save the data as a csv and remove the pandas index away!

    • @Algovibes
      @Algovibes  3 роки тому +1

      @@lilymaths4584 awesome :-)

  • @edsonquam4954
    @edsonquam4954 2 роки тому +1

    Hi!!! Nice video … d’ont you have a "discord " community?? If yes please , the Access link…. Thanks!!!

    • @Algovibes
      @Algovibes  2 роки тому

      Hi 👋
      Thanks a a lot for you comment. Unfortunately, I haven't. Sorry!

  • @nikeairforce9893
    @nikeairforce9893 Рік тому +1

    HEY man, you are 100% a German :=), right?

    • @Algovibes
      @Algovibes  Рік тому

      🤫 Please don't tell anyone

    • @nikeairforce9893
      @nikeairforce9893 Рік тому +1

      @@Algovibes Diesen Akzent kriegt man aber auch nicht raus, wa? haha

    • @Algovibes
      @Algovibes  Рік тому

      @@nikeairforce9893 🥹

  • @DM-py7pj
    @DM-py7pj 2 роки тому +1

    Are there no schemas? E.g. dbo.employees?

    • @Algovibes
      @Algovibes  2 роки тому +1

      Good question! In sqlite there are no schemas. So the simple answer is: No. Instead of a schema you would need to create a new database file.
      If you want to work with schemas I covered MySQL in my stock recommendation series, might be interesting for you:
      ua-cam.com/video/qGAUw63p_uk/v-deo.html

    • @DM-py7pj
      @DM-py7pj 2 роки тому +1

      @@Algovibes thank you