How to Convert SQL into Python Pandas? Part 7 - Pivot & Unpivot
Previously on How to Convert SQL into Python Pandas? Part 6, we talked about how to convert SQL statements into Pandas syntax. In this article, we will continue to discover more scenarios and operations.
Let’s create a simple table (data frame) to explain pivot/ unpivot ideas.
-- SQL
CREATE VIEW main.invoices_country AS
SELECT *
FROM main.invoices
WHERE BillingCountry IN ('Germany', 'USA')
# Python Pandas
from google.colab import drive
drive.mount('/content/drive')
import sqlite3
import pandas as pd
import numpy as np
con = sqlite3.connect('/content/drive/MyDrive/Data/db/sample.db')
df_invoices_country = pd.read_sql("select * from invoices where BillingCountry in ('Germany', 'USA')", con)
Keep reading with a 7-day free trial
Subscribe to Informula to keep reading this post and get 7 days of free access to the full post archives.