VNSGU BCA Sem 2: Programming Skills - Advanced (204_02) Practical Solutions - Set D¶
Paper Details
- Subject: Programming Skills - Advanced (PKUP)
- Subject Code: 204_02
- Set: D
- Semester: 2
- Month/Year: April 2025
- Max Marks: 25
- Time Recommendation: 45 Minutes
- Paper: View Paper | Download PDF
Questions & Solutions¶
All questions are compulsory¶
Q1A: Product Data Pipeline¶
Max Marks: 20
Using Python, perform the following tasks for PRODUCT data management:
1. Create a dictionary (or a set of sets) containing product details with the following fields: PRODUCT_ID, PRODUCT_NAME, CATEGORY, PRICE, STOCK_QUANTITY. Add at least 10 product records.
2. Convert the dictionary into a pandas DataFrame.
3. Save the DataFrame as a CSV file named product_data.csv.
4. Load the CSV file and display:
a) All products in the "Electronics" category
b) Products priced below 100
c) Products with stock quantity less than 20
1. Dictionary & DataFrame Creation¶
Initialize the product inventory and convert it into a Pandas DataFrame.
Hint
Define a dictionary where keys are the required fields and values are lists of 10 items. Use import pandas as pd and pd.DataFrame() for the conversion.
flowchart TD
dict[Product Dictionary]
df[Pandas DataFrame]
conv[pd.DataFrame Conversion]
dict --> conv
conv --> df
View Solution & Output
import pandas as pd
# [1] Create dictionary with product details
product_dict = {
'PRODUCT_ID': ['P001', 'P002', 'P003', 'P004', 'P005', 'P006', 'P007', 'P008', 'P009', 'P010'],
'PRODUCT_NAME': ['Mouse', 'Keyboard', 'Laptop', 'Monitor', 'USB Cable', 'Webcam', 'Headphones', 'Tablet', 'Router', 'Pen Drive'],
'CATEGORY': ['Electronics', 'Electronics', 'Electronics', 'Electronics', 'Accessories', 'Electronics', 'Accessories', 'Electronics', 'Networking', 'Accessories'],
'PRICE': [25, 45, 1200, 150, 10, 60, 80, 300, 50, 15],
'STOCK_QUANTITY': [50, 30, 15, 10, 100, 5, 25, 12, 40, 80]
}
# [2] Convert dictionary into pandas DataFrame
df = pd.DataFrame(product_dict)
print("DataFrame successfully initialized.")
print(df.head())
Step-by-Step Explanation:
1. Initialization: Create the product_dict dictionary with inventory data and import the pandas library.
2. Logic Flow: Convert the raw dictionary into a structured DataFrame df using pd.DataFrame().
3. Completion: Use the head() method to output the first five rows and confirm everything is correctly formatted.
2. File Storage (CSV)¶
Save the product data into a CSV file.
Hint
Use df.to_csv('product_data.csv', index=False).
flowchart TD
df[Pandas DataFrame]
save[Save to CSV]
file[product_data.csv]
df --> save
save --> file
View Solution & Output
# [3] Save DataFrame as CSV file
df.to_csv('product_data.csv', index=False)
print("File 'product_data.csv' created successfully.")
Step-by-Step Explanation:
1. Initialization: Select the DataFrame df which contains the latest product records.
2. Logic Flow: Export the data to a file named 'product_data.csv' using to_csv() with index=False.
3. Completion: A local CSV file is created on the disk for record-keeping and storage.
3. Data Loading & Analytics¶
Load the CSV and perform filtering.
Hint
Use filtering syntax like df[df['CATEGORY'] == 'Electronics'].
flowchart TD
load[Load read_csv]
filter[Apply Filters]
show[Show Results]
load --> filter
filter --> show
View Solution & Output
# [4] Load CSV file and perform queries
load_df = pd.read_csv('product_data.csv')
# Query 1: Category Electronics
print("\n[a] Electronics Products:")
print(load_df[load_df['CATEGORY'] == 'Electronics'])
# Query 2: Price < 100
print("\n[b] Budget Products (Price < 100):")
print(load_df[load_df['PRICE'] < 100])
# Query 3: Stock Quantity < 20
print("\n[c] Low Stock Alerts (Stock < 20):")
print(load_df[load_df['STOCK_QUANTITY'] < 20])
Step-by-Step Explanation:
1. Initialization: Use pd.read_csv() to bring the product data back into a DataFrame for analysis.
2. Logic Flow: Apply boolean filtering based on category (Electronics), price threshold (100), and low stock counts (20).
3. Completion: Display the results for each specific business query to track inventory and sales.
Q2: Viva Preparation¶
Max Marks: 5
Potential Viva Questions
- Q: What is Pandas?
- A: Pandas is a Python library used for data manipulation and analysis, providing high-performance data structures like Series and DataFrames.
- Q: How do you add a new column to a DataFrame?
- A: By assigning a list or Series to a new column name, e.g.,
df['Total_Value'] = df['PRICE'] * df['STOCK_QUANTITY']. - Q: What does
df.describe()do? - A: It generates descriptive statistics that summarize the central tendency, dispersion, and shape of a dataset’s distribution (like mean, min, max).
- Q: How do you access a specific cell in a DataFrame?
- A: Use
.at[row_label, 'column_name']or.iat[row_index, col_index]for scalar access. - Q: What is the difference between a Series and a DataFrame?
- A: A Series is a 1D labeled array (like a single column), while a DataFrame is a 2D labeled tabular structure (like an entire table).
- Q: How do you handle missing numeric data in Pandas?
- A: Use
df.fillna(0)to replace NaNs with zero ordf.dropna()to remove rows with missing values.
Common Pitfalls
- Incorrect Column Names: Python is case-sensitive.
df['price']will fail if the column is namedPRICE. - Data Types after Loading: Sometimes numeric columns might be loaded as objects if there's non-numeric data. Use
df.info()to verify.
Quick Navigation¶
Related Solutions¶
| Set | Link |
|---|---|
| Set A | Solutions |
| Set B | Solutions |
| Set C | Solutions |
| Set D | Current Page |
| Set E | Solutions |
| Set F | Solutions |
Last Updated: April 2025