Git Product home page Git Product logo

dsc-1-05-16-using-sql-with-pandas's Introduction

Using SQL with Pandas

Introduction

Consider the structure of a Pandas DataFrame.

Now, let's consider the structure of a table from a SQL database.

You've probably noticed by now that they're essentially the same--a table of values, with each row having a unique index and each column having a unique name. This allows us to quickly and easily access information when using SQL. In this section, we'll learn how we can use SQL-style queries to query pandas DataFrames!

Objectives

You will be able to:

  • Understand the basic syntax for querying pandas DataFrames with SQL statements
  • Understand and explain how Pandas can be used to work directly with .sql files
  • Reference the pandasql package documentation as needed

Using .query()

Pandas DataFrames come with a built in query method, which allows us to get information from DataFrames quickly without using the cumbersome slicing syntax.

See the following examples:

# Getting Data using slicing syntax
foo_df = bar_df[bar_df[bar_df['Col_1'] > bar_df['Col_2']]]

# Using The query method
foo_df = bar_df.query("Col_1 > Col_2")

# These two lines are equivalent!

Note that if we want to use and and or statements with the .query() method, we'll need to use "&" and "|" instead.

foo_df = bar_df.query("Col_1 > Col_2 & Col_2 <= Col_3")

Using SQL syntax with pandasql

Since SQL is such a powerful, comfortable tool for Data Scientists, some people had the bright idea of creating a library that lets users query DataFrames using SQL-style syntax. This library is called pandasql.

We can install pandasql using the bash comman pip install pandasql.

Importing pandasql

In order to use pandasql, we need to start by importing a sqldf object from pandasql

from pandasql import sqldf

Next, we'll write a lambda function that will make it quicker and easier to write queries. Normally, we would have to pass in the global variables every time we use an object. In order to avoid doing this every time, we'll write a lambda that does this for us.

pysqldf = lambda q: sqldf(q, globals())

Now, when we pass a query into pysqldf, the lambda will also pass along the globals for us, saving us that repetitive task.

Writing Queries

To write a query, we just format it as a multi-line string!

q = """SELECT
        m.date, m.beef, b.births
     FROM
        meats m
     INNER JOIN
        births b
           ON m.date = b.date;"""

In order to query DataFrames, we can just pass in the query string we've created to our sqldf object that we stored in pysqldf. This will return a DataFrame.

results = pysqldf(q)

Summary

These advanced methods for querying DataFrames can make your life a lot easier by simplifying the syntax and allowing us to make use of SQL--use them to save yourself time and give keep your SQL skills strong!

dsc-1-05-16-using-sql-with-pandas's People

Contributors

loredirick avatar mike-kane avatar peterbell avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.