VNSGU BCA Sem 2: Programming Skills - Advanced (204_02) Practical Solutions - Set B¶
Paper Details
- Subject: Programming Skills - Advanced (PKUP)
- Subject Code: 204_02
- Set: B
- 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: Library Book Pipeline¶
Max Marks: 20
Using Python, perform the following tasks for LIBRARY BOOK data management:
1. Create a dictionary containing book details (BOOK_ID, TITLE, AUTHOR, GENRE, AVAILABILITY, ADDED_DATE). Add at least 10 book records.
2. Convert the dictionary into a pandas DataFrame.
3. Save the DataFrame as a CSV file named library_books.csv.
4. Load the CSV file and display specific queries (History, Available, Date > 2020).
1. Dictionary & DataFrame Creation¶
Initialize the library dataset and convert it into a Pandas DataFrame.
Hint
Define a dictionary where each key is a column and the value is a list of 10 items. Use pd.DataFrame() to transform it.
flowchart TD
dict[Library Dictionary]
df[Pandas DataFrame]
conv[pd.DataFrame Conversion]
dict --> conv
conv --> df
View Solution & Output
import pandas as pd
# [1] Create dictionary with book details
library_data = {
'BOOK_ID': ['LIB001', 'LIB002', 'LIB003', 'LIB004', 'LIB005', 'LIB006', 'LIB007', 'LIB008', 'LIB009', 'LIB010'],
'TITLE': ['Py Programming', 'Data Structures', 'Indian History', 'World War II', 'ML Basics', 'Ancient Civilizations', 'Modern India', 'AI', 'Medieval History', 'Deep Learning'],
'AUTHOR': ['John Smith', 'Robert Sedgewick', 'Bipin Chandra', 'Winston Churchill', 'Andrew Ng', 'Will Durant', 'Ramachandra Guha', 'Stuart Russell', 'Barbara Tuchman', 'Ian Goodfellow'],
'GENRE': ['Tech', 'Tech', 'History', 'History', 'Tech', 'History', 'History', 'Tech', 'History', 'Tech'],
'AVAILABILITY': [True, True, False, True, True, True, False, True, True, False],
'ADDED_DATE': ['2019-03-15', '2020-01-20', '2018-11-10', '2021-05-25', '2022-08-12', '2020-07-18', '2019-09-30', '2023-02-14', '2021-03-22', '2022-12-01']
}
# [2] Convert dictionary into pandas DataFrame
df = pd.DataFrame(library_data)
print("DataFrame Created Successfully!")
Step-by-Step Explanation:
1. Initialization: Import the pandas library and define a dictionary library_data with 10 book records and details.
2. Logic Flow: Use pd.DataFrame() to transform the structured dictionary into a Pandas DataFrame.
3. Completion: Display a success message confirming the initialization of the library dataset.
2. File Storage (CSV)¶
Save the library records into a persistent CSV file for data sharing.
Hint
Use df.to_csv() with index=False. This prevents Pandas from adding a default numeric index column to your file.
flowchart TD
df[Pandas DataFrame]
csv[Save To CSV]
file[library_books.csv]
df --> csv
csv --> file
View Solution & Output
# [3] Save DataFrame as CSV
df.to_csv('library_books.csv', index=False)
print("File 'library_books.csv' created.")
Step-by-Step Explanation:
1. Initialization: Use the existing df DataFrame containing the book information.
2. Logic Flow: Export the data to a CSV file named 'library_books.csv' using to_csv() with index=False.
3. Completion: A persistent CSV file is created on the disk for data sharing and long-term storage.
3. Data Loading & Filtering¶
Reload the CSV and search for specific books based on genre and date added.
Hint
Filter using conditions like df[df['GENRE'] == 'History']. Convert the Date column using pd.to_datetime() for accurate filtering.
flowchart TD
load[Load read_csv]
date[Convert to Datetime]
filter[Apply Queries]
show[Show Results]
load --> date
date --> filter
filter --> show
View Solution & Output
# [4] Load CSV and perform queries
load_df = pd.read_csv('library_books.csv')
load_df['ADDED_DATE'] = pd.to_datetime(load_df['ADDED_DATE'])
# Query 1: History Genre
print("\n[a] History Books:")
print(load_df[load_df['GENRE'] == 'History'])
# Query 2: Availability
print("\n[b] Available Books:")
print(load_df[load_df['AVAILABILITY'] == True])
# Query 3: Date > 2020-06-01
print("\n[c] Books added after June 2020:")
print(load_df[load_df['ADDED_DATE'] > '2020-06-01'])
Step-by-Step Explanation:
1. Initialization: Load the library data using pd.read_csv() and convert the ADDED_DATE column to datetime objects.
2. Logic Flow: Perform filtering operations to find books by genre (History), availability, and specific addition date range.
3. Completion: Display the results for each filter to the console to analyze the available book inventory.
Q2: Viva Preparation¶
Max Marks: 5
Potential Viva Questions
- Q: How do you add a new column to an existing DataFrame?
- A: Use
df['new_column_name'] = list_of_valuesordf.assign(). - Q: What is the difference between
to_csv()andread_csv()? - A:
to_csv()exports data from Python to a file;read_csv()imports data from a file into Python. - Q: Why did we use
index=Falseinto_csv()? - A: To prevent Pandas from writing an extra, unnamed column for the row numbers into our CSV file.
- Q: How can you see only the first 5 rows of a large DataFrame?
- A: Use the
df.head()method. - Q: Why use
index=Falseinto_csv()? - A: To prevent Pandas from adding an extra column for row numbers in our final CSV file.
- Q: How can you check for duplicate records?
- A: Use
df.duplicated().sum()to count duplicate rows. - Q: What is the purpose of
read_csv()? - A: It loads data from a CSV file into a Pandas DataFrame for analysis.
- Q: How do you filter data based on multiple conditions?
- A: Use bitwise operators
&(AND) or|(OR), e.g.,df[(df['Genre'] == 'History') & (df['Availability'] == True)]. - Q: What does
df.info()provide? - A: It provides a concise summary of the DataFrame, including the number of non-null entries and data types of each column.
- Q: How can you find unique values in a column?
- A: Use the
df['column_name'].unique()method.
Common Pitfalls
- Path Issues: Forgetting to use
.csvextension when saving. - Case Sensitivity: Searching for "history" instead of "History" in the genre filter will return zero results.
- Boolean Values:
AVAILABILITYshould beTrue/False(unquoted) in the dict, but might become1/0orTrue/Falsestrings in some CSV parsers. Use== Truefor safety. - Date Format: Standard strings like "2020-06-01" are treated as plain text unless explicitly converted using
pd.to_datetime().
Quick Navigation¶
Related Solutions¶
| Set | Link |
|---|---|
| Set A | Solutions |
| Set B | Current Page |
| Set C | Solutions |
| Set D | Solutions |
| Set E | Solutions |
| Set F | Solutions |
Last Updated: April 2025