Book Image

Learning pandas - Second Edition

By : Michael Heydt
Book Image

Learning pandas - Second Edition

By: Michael Heydt

Overview of this book

You will learn how to use pandas to perform data analysis in Python. You will start with an overview of data analysis and iteratively progress from modeling data, to accessing data from remote sources, performing numeric and statistical analysis, through indexing and performing aggregate analysis, and finally to visualizing statistical data and applying pandas to finance. With the knowledge you gain from this book, you will quickly learn pandas and how it can empower you in the exciting world of data manipulation, analysis and science.
Table of Contents (16 chapters)

Reading and writing from/to SQL databases

Pandas can read data from any SQL database that supports Python data adapters that respect the Python DB-API. Reading is performed using the pandas.io.sql.read_sql() function, and writing to SQL databases is done using the .to_sql() method of DataFrame.

To demonstrate, the following reads the stock data from msft.csv and aapl.csv. It then makes a connection to an SQLite3 database file. If the file does not exist, it is created on the fly. It then writes the MSFT data to a table named STOCK_DATA. If the table does not exist, it is created as well. If it does exist, all the data is replaced with the MSFT data. Finally, it then appends the AAPL stock data to that table:

To demonstrate that this data was created, you can open the database file with a tool such as SQLite Data Browser (available at https://github.com/sqlitebrowser/sqlitebrowser...