VNSGU BCA Sem 2: Programming Skills - Advanced (204_02) Practical Solutions - Set A¶
Paper Details
- Subject: Programming Skills - Advanced (PKUP)
- Subject Code: 204_02
- Set: A
- 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: Vehicle Data Pipeline¶
Max Marks: 20
Using Python, perform the following tasks for VEHICLE REGISTRATION data management:
1. Create a dictionary containing vehicle details (REG_NO, OWNER_NAME, VEHICLE_TYPE, MODEL_YEAR, CITY, REGISTRATION_DATE). Add at least 10 vehicle records.
2. Convert the dictionary into a pandas DataFrame.
3. Save the DataFrame as a CSV file named vehicle_data.csv.
4. Load the CSV file and display specific queries (City == Ahmedabad, Year > 2018, Date > 2022-01-01).
1. Dictionary & DataFrame Creation¶
Initialize the vehicle data and convert it into a structured DataFrame.
Hint
A dictionary with keys as column names and values as lists is the best way to load data into Pandas. Use pd.DataFrame(dict_name) to convert.
flowchart TD
dict[Vehicle Dictionary]
df[Pandas DataFrame]
conv[pd.DataFrame Conversion]
dict --> conv
conv --> df
View Solution & Output
import pandas as pd
# [1] Create dictionary with vehicle details
vehicle_dict = {
'REG_NO': ['GJ01A1', 'GJ02B2', 'GJ01C3', 'GJ05D4', 'GJ01E5', 'GJ03F6', 'GJ01G7', 'GJ04H8', 'GJ01I9', 'GJ06J0'],
'OWNER_NAME': ['Rahul', 'Priya', 'Amit', 'Sneha', 'Vikram', 'Neha', 'Raj', 'Pooja', 'Karan', 'Divya'],
'VEHICLE_TYPE': ['Car', 'Bike', 'Car', 'SUV', 'Bike', 'Car', 'SUV', 'Car', 'Bike', 'Car'],
'MODEL_YEAR': [2019, 2020, 2021, 2018, 2022, 2023, 2019, 2021, 2020, 2022],
'CITY': ['Ahmedabad', 'Surat', 'Ahmedabad', 'Vadodara', 'Ahmedabad', 'Rajkot', 'Ahmedabad', 'Surat', 'Vadodara', 'Ahmedabad'],
'REGISTRATION_DATE': ['2019-05-15', '2020-03-20', '2021-08-10', '2018-12-25', '2022-06-30', '2023-01-15', '2019-11-05', '2021-04-18', '2020-09-22', '2022-12-01']
}
# [2] Convert dictionary into pandas DataFrame
df = pd.DataFrame(vehicle_dict)
print("DataFrame Created Successfully!")
print(df.head())
Step-by-Step Explanation:
1. Initialization: Import pandas as pd and define the vehicle_dict with 10 records containing registration details.
2. Logic Flow: Use pd.DataFrame() to convert the dictionary into a tabular format and store it in the variable df.
3. Completion: Display a confirmation message and the first five rows of the DataFrame using head() to verify the data.
2. File Storage (CSV)¶
Save the records into a persistent file for later use.
Hint
Use the to_csv() method. Set index=False to prevent Pandas from adding an extra column for row indices.
flowchart TD
df[Pandas DataFrame]
csv[CSV File Storage]
disk[Save to vehicle_data.csv]
df --> csv
csv --> disk
View Solution & Output
# [3] Save DataFrame as CSV file
df.to_csv('vehicle_data.csv', index=False)
print("File 'vehicle_data.csv' created.")
Step-by-Step Explanation:
1. Initialization: Access the existing DataFrame df that contains the vehicle registration records.
2. Logic Flow: Call the to_csv() method on the DataFrame, specifying 'vehicle_data.csv' as the filename and setting index=False to omit row numbers.
3. Completion: Verify that the file has been successfully created in the local directory with a confirmation print statement.
3. Data Loading & Queries¶
Retrieve the data and perform advanced filtering based on criteria.
Hint
Use pd.read_csv() to load. For date-based queries, ensure the column is converted using pd.to_datetime().
flowchart TD
load[Load read_csv]
date[Convert to Datetime]
filter[Apply Filters]
show[Display Results]
load --> date
date --> filter
filter --> show
View Solution & Output
# [4] Load CSV file and perform queries
load_df = pd.read_csv('vehicle_data.csv')
load_df['REGISTRATION_DATE'] = pd.to_datetime(load_df['REGISTRATION_DATE'])
# Query 1: City Ahmedabad
print("\n[a] Vehicles in Ahmedabad:")
print(load_df[load_df['CITY'] == 'Ahmedabad'])
# Query 2: Model Year > 2018
print("\n[b] Vehicles after 2018:")
print(load_df[load_df['MODEL_YEAR'] > 2018])
# Query 3: Date > 2022-01-01
print("\n[c] Registration after 2022:")
print(load_df[load_df['REGISTRATION_DATE'] > '2022-01-01'])
Step-by-Step Explanation:
1. Initialization: Import the data from vehicle_data.csv using pd.read_csv() and convert the registration date strings into proper datetime objects.
2. Logic Flow: Apply boolean indexing to the DataFrame to filter records based on city name, manufacturing year, and specific registration date.
3. Completion: Print the results of each query to the console to view the filtered subsets of vehicle data.
Q2: Viva Preparation¶
Max Marks: 5
Potential Viva Questions
- Q: What is a DataFrame in Pandas?
- A: A two-dimensional, size-mutable, and potentially heterogeneous tabular data structure with labeled axes.
- Q: Why use
pd.to_datetime()? - A: It converts strings into datetime objects, which allows for advanced filtering (e.g., finding dates after a specific year).
- Q: Difference between
locandiloc? - A:
locis label-based (using column names), whileilocis integer-index based (using row/column numbers). - Q: How can you check for missing values in a DataFrame?
- A: Use the
df.isnull().sum()method. - Q: What is the benefit of saving data to CSV?
- A: CSV is a lightweight, universal format that can be read by almost any data tool, including Excel.
- Q: How do you add a new row to a DataFrame?
- A: You can use
df.loc[len(df)] = [new_values]orpd.concat()for larger merges.
Common Pitfalls
- Missing Index Rule: Forgetting
index=Falsewhen saving to CSV while doing data processing. - Date Format: Standard strings like "2022-01-01" are treated as plain text unless explicitly converted to datetime objects.
Quick Navigation¶
Related Solutions¶
| Set | Link |
|---|---|
| Set A | Current Page |
| Set B | Solutions |
| Set C | Solutions |
| Set D | Solutions |
| Set E | Solutions |
| Set F | Solutions |
Last Updated: April 2025