Accessing databases from pandas

We can give pandas a database connection such as the one in the previous example or a SQLAlchemy connection. We will cover the latter in the later sections of this chapter. We will load the statsmodels sunactivity data, just like in the previous chapter, Chapter 7, Signal Processing and Time Series:

  1. Create a list of tuples to form the pandas DataFrame:
    rows = [tuple(x) for x in df.values]

    Contrary to the previous example, create a table without specifying data types:

    con.execute("CREATE TABLE sunspots(year, sunactivity)")
  2. The executemany() method executes multiple statements; in this case, we will be inserting records from a list of tuples. Insert all the rows into the table and show the row count as follows:
    con.executemany("INSERT ...

Get Python Data Analysis now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.