Welcome to Foundation of Data Science Laboratory
Welcome to Foundation of Data Science Laboratory
3. Importing Data from SQL Databases:
o Set up a local SQLite database and create a table with some sample data.
o Use Python to import data from the SQL table into a DataFrame.
o Display the first few rows of the DataFrame
Step 1: Set Up SQLite Database and Create a Table
import sqlite3
# Connect to SQLite database (or create it if it doesn't exist)
conn = sqlite3.connect('sample_db.sqlite')
# Create a cursor object
cursor = conn.cursor()
# Create a table
cursor.execute('''
CREATE TABLE IF NOT EXISTS liar_sample (
id INTEGER PRIMARY KEY,
label TEXT,
statement TEXT,
speaker TEXT,
context TEXT,
job_title TEXT,
state TEXT
)
''')
# Insert some sample data
cursor.executemany('''
INSERT INTO liar_sample (label, statement, speaker, context, job_title, state)
VALUES (?, ?, ?, ?, ?, ?)
''', [
('false', 'Says the Annies List political group supports third-trimester abortions on demand.', 'Greg Abbott', 'Texas', 'Governor', 'Texas'),
('mostly-true', 'When asked about Rick Perry\'s ad on romerocares...', 'Rick Perry', 'Washington', 'Governor', 'Washington')
])
# Commit the transaction
conn.commit()
Step 2: Import Data from SQLite Table into a Pandas DataFrame
import pandas as pd
# Query the data from the SQL table
df = pd.read_sql_query("SELECT * FROM liar_sample", conn)
# Display the first few rows of the DataFrame
df.head()
Step 3: Close the Database Connection
# Close the database connection
conn.close()
id label statement speaker context job_title state
0 1 false Says the Annies List political group supports t... Greg Abbott Texas Governor Texas
1 2 mostly-true When asked about Rick Perry's ad on romerocares... Rick Perry Washington Governor Washington
SQLite Database Setup:
The sqlite3 module is used to create a SQLite database named sample_db.sqlite.
A table called liar_sample is created with columns corresponding to those in the LIAR dataset.
Two rows of sample data are inserted into this table.
Data Import to DataFrame:
The pandas library is used to query data from the SQLite table and import it into a DataFrame.
The first few rows of the DataFrame are displayed using df.head().
Closing the Connection:
It's a good practice to close the connection to the database once the operations are completed.