VNSGU BCA Sem 2: Data Analysis Using Python (205_04) Practical Solutions - April 2025 Set C
- Subject: Data Analysis Using Python (DAUP)
- Subject Code: 205_04
- Set: C
- Semester: 2
- Month/Year: April 2025
- Max Marks: 25
- Time Recommendation: 45 Minutes
- Paper: View Paper | Download PDF
Questions & Solutions
All questions are compulsory
Q1: Order Data Analysis Pipeline
Max Marks: 20
Create a table ORDERS in SQLite with the following columns: ORDER_ID, CUSTOMER_NAME, PRODUCT_NAME, QUANTITY, TOTAL_PRICE, ORDER_DATE using Python.
- Insert 10 records into the table.
- Display records where the total price is greater than 500.
- Export order data and convert it into a Data Frame.
- Convert the Data Frame into
orders.csvfile and save it.
1. SQLite Table Creation & Data Entry
Set up the sales database and populate it with order history.
In SQLite, ORDER is a reserved keyword. Use double quotes "ORDERS" or a different table name to avoid syntax errors.
View Solution & Output
import sqlite3
# [1] Create database and table
conn = sqlite3.connect('sales.db')
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS ORDERS (
ORDER_ID INTEGER PRIMARY KEY,
CUSTOMER_NAME TEXT,
PRODUCT_NAME TEXT,
QUANTITY INTEGER,
TOTAL_PRICE REAL,
ORDER_DATE TEXT
)
''')
# Insert 10 records
orders_data = [
(101, 'Rahul', 'Laptop', 1, 55000, '2024-05-01'),
(102, 'Priya', 'Mouse', 2, 800, '2024-05-02'),
(103, 'Amit', 'Keyboard', 1, 1200, '2024-05-03'),
(104, 'Sia', 'USB Hub', 1, 450, '2024-05-04'),
(105, 'Aryan', 'Monitor', 1, 8500, '2024-05-05'),
(106, 'Neha', 'Cables', 3, 300, '2024-05-06'),
(107, 'Vikram', 'Printer', 1, 12000, '2024-05-07'),
(108, 'Sneha', 'Webcam', 1, 2500, '2024-05-08'),
(109, 'Karan', 'Pen Drive', 5, 1500, '2024-05-09'),
(110, 'Mira', 'Earphones', 1, 499, '2024-05-10')
]
cursor.executemany('INSERT INTO ORDERS VALUES (?,?,?,?,?,?)', orders_data)
conn.commit()
print("Table created and 10 records inserted.")
Step-by-Step Explanation:
- Initialization: Establish a connection to
sales.dband create a cursor object for database operations. - Logic Flow: Execute a SQL command to create the
ORDERStable and useexecutemany()to insert 10 sample records efficiently. - Completion: Commit the transaction to save changes and print a success message.
2. High Value Order Query
Identify and display orders exceeding 500 in total price.
Use the SQL WHERE clause: SELECT * FROM ORDERS WHERE TOTAL_PRICE > 500.
View Solution & Output
# [2] Display records where total price > 500
cursor.execute('SELECT * FROM ORDERS WHERE TOTAL_PRICE > 500')
high_value_orders = cursor.fetchall()
print("\nOrders with Total Price > 500:")
for row in high_value_orders:
print(row)
Step-by-Step Explanation:
- Initialization: Use the existing database cursor to prepare a selection query.
- Logic Flow: Execute a
SELECTstatement with aWHEREclause to filter orders whereTOTAL_PRICEexceeds 500. - Completion: Fetch all matching results and iterate through them to display each record.
3. Data Export to Pandas & CSV
Generate a portable CSV file for reporting using the Pandas library.
df.to_csv() saves the current state of the DataFrame to a text-based CSV format.
View Solution & Output
import pandas as pd
# [3] Export to Data Frame
df = pd.read_sql_query('SELECT * FROM ORDERS', conn)
print("\nData Frame View:")
print(df.head())
# [4] Convert to CSV
df.to_csv('orders.csv', index=False)
print("\nFile 'orders.csv' saved successfully.")
conn.close()
Step-by-Step Explanation:
- Initialization: Import the
pandaslibrary to handle data manipulation and conversion. - Logic Flow: Use
pd.read_sql_query()to pull all data from theORDERStable directly into a DataFrame. - Completion: Save the DataFrame to a file named
orders.csvusingto_csv()and close the database connection.
Concept Deep Dive: Reserved Keywords
In SQL, words like ORDER, SELECT, and TABLE are "reserved" for commands. If you name your table ORDER, you might get syntax errors. It's best practice to use plural names like ORDERS or enclose them in double quotes "ORDER" to tell SQLite it's a name, not a command.
Q2: Viva Preparation
Max Marks: 5
Potential Viva Questions
- Q: How do you check the data types of columns in a DataFrame?
- A: Use
df.dtypes.
- A: Use
- Q: What is the difference between
read_sqlandread_sql_query?- A:
read_sqlis a wrapper around bothread_sql_tableandread_sql_query.read_sql_queryis specifically for executing SQL strings.
- A:
- Q: How do you rename a column in Pandas?
- A: Use
df.rename(columns={'OLD_NAME': 'NEW_NAME'}).
- A: Use
- Q: Why use
REALinstead ofINTEGERfor total price?- A:
REALsupports floating-point (decimal) numbers, which are necessary for currency and prices.
- A:
- Q: How do you select rows with multiple conditions in SQL?
- A: Use the
ANDorORkeywords, e.g.,SELECT * FROM ORDERS WHERE TOTAL_PRICE > 500 AND QUANTITY < 5.
- A: Use the
- Q: What is the purpose of
df.to_dict()?- A: It converts the DataFrame back into a Python dictionary, which can be useful for integration with other scripts or APIs.
- Floating Point Comparison: When filtering prices like
> 500, ensure the data is stored asREALorFLOATto handle decimals correctly. - CSV Path: Ensure you have write permissions in the folder where you are saving the CSV.
Quick Navigation
Related Solutions
| Set | Link |
|---|---|
| Set A | Solutions |
| Set B | Solutions |
| Set C | Current Page |
| Set D | Solutions |
| Set E | Solutions |
| Set F | Solutions |
Last Updated: April 2026