SQL क्वेरी के साथ imports को refine करना

pandas के साथ सरल Data Ingestion

Amany Mahfouz

Instructor

कॉलम SELECT करना

  • SELECT [column names] FROM [table name];
  • उदाहरण:
    SELECT date, tavg 
    FROM weather;
    
pandas के साथ सरल Data Ingestion

WHERE क्लॉज़

  • रिकॉर्ड्स को चुनिंदा लाने के लिए WHERE क्लॉज़ का उपयोग करें
    SELECT [column_names] 
      FROM [table_name] 
     WHERE [condition];
    
pandas के साथ सरल Data Ingestion

नंबरों से फ़िल्टर करना

  • संख्याओं की तुलना गणितीय ऑपरेटर से करें
    • =
    • > और >=
    • < और <=
    • <> (not equal to)
    • उदाहरण:
      SELECT * 
      FROM weather 
      WHERE tmax > 32;
      
pandas के साथ सरल Data Ingestion

टेक्स्ट फ़िल्टर करना

  • = चिन्ह और मिलाने वाला टेक्स्ट देकर exact स्ट्रिंग मैच करें
  • स्ट्रिंग मैचिंग case-sensitive होती है
  • उदाहरण:
    /* Get records about incidents in Brooklyn */
    SELECT * 
    FROM hpd311calls
    WHERE borough = 'BROOKLYN';
    
pandas के साथ सरल Data Ingestion

SQL और pandas

# Load libraries
import pandas as pd
from sqlalchemy import create_engine

# Create database engine engine = create_engine("sqlite:///data.db")
# Write query to get records from Brooklyn query = """SELECT * FROM hpd311calls WHERE borough = 'BROOKLYN';"""
# Query the database brooklyn_calls = pd.read_sql(query, engine)
print(brookyn_calls.borough.unique())
['BROOKLYN']
pandas के साथ सरल Data Ingestion

शर्तें जोड़ना: AND

  • AND के साथ WHERE क्लॉज़ वे रिकॉर्ड लौटाते हैं जो सभी शर्तें पूरी करें
# Write query to get records about plumbing in the Bronx
and_query = """SELECT * 
                 FROM hpd311calls 
                WHERE borough = 'BRONX' 
                  AND complaint_type = 'PLUMBING';"""

# Get calls about plumbing issues in the Bronx bx_plumbing_calls = pd.read_sql(and_query, engine) # Check record count print(bx_plumbing_calls.shape)
(2016, 8)
pandas के साथ सरल Data Ingestion

शर्तें जोड़ना: OR

  • OR के साथ WHERE क्लॉज़ वे रिकॉर्ड लौटाते हैं जो कम-से-कम एक शर्त पूरी करें
# Write query to get records about water leaks or plumbing
or_query = """SELECT * 
                FROM hpd311calls 
               WHERE complaint_type = 'WATER LEAK'
                  OR complaint_type = 'PLUMBING';"""

# Get calls that are about plumbing or water leaks leaks_or_plumbing = pd.read_sql(or_query, engine) # Check record count print(leaks_or_plumbing.shape)
(10684, 8)
pandas के साथ सरल Data Ingestion

अभ्यास करते हैं!

pandas के साथ सरल Data Ingestion

Preparing Video For Download...