Each run starts with fresh data. Download any exported files before another run. Your written answers and code drafts are saved on this device; export your work to keep a backup.
M20C01 · Explore · predict · practice
Read and export CSV
CSV stores text rows separated by delimiters. read_csv creates a DataFrame and attempts to infer column types. to_csv writes the selected data; index=False avoids saving the pandas row index as an extra column.
The report contains S004, S008, and S011. All written files go in outputs.
Compare your explanations
Read. A platform-independent path to a file inside the data directory.
Predict. 4 students: S001, S003, S006, and S010.
Explain. IDs are labels, not quantities; text preserves formats such as leading zeros.
One quick check
Why use index=False on export?
What you should be able to explain. Inspect imports and choose export columns deliberately.
M20C02 · Explore · predict · practice
Move records through JSON
JSON can store a list of records, each represented by a dictionary. orient="records" makes this structure explicit. Re-reading a file checks that the exported shape matches what you intended.
The round trip preserves these two records and their field names.
Compare your explanations
Read. One student row, with column names as dictionary keys.
Predict. 3, because head(3) selects three rows.
Explain. It shows the actual file structure, not only its DataFrame representation.
One quick check
What does orient="records" produce?
What you should be able to explain. Choose a file orientation that matches the receiving system.
M20C03 · Explore · predict · practice
Select an Excel worksheet
An Excel workbook can contain several sheets. sheet_name selects one by name. The openpyxl engine handles the provided .xlsx workbook and is listed in the installation requirements.
Watch for this. If openpyxl is missing, install the provided requirements before running this cell.
Your turn
Read the Students sheet. Store a summary in result with columns Program and Students, where Students counts rows per program. Export it to program_counts.xlsx, sheet Counts.
For the code check, keep these result variable names: result. Different valid approaches are welcome.
Show a hint
groupby("Program").size() counts rows; name the count column.
The workbook contains two counts: six AI students and six IT students.
Compare your explanations
Read. students.xlsx is the workbook file; Students identifies a worksheet inside it.
Predict. 2, one for Dubai and one for RAK.
Explain. It preserves the supplied source data and clearly identifies the generated report.
One quick check
Which argument chooses a worksheet?
What you should be able to explain. Excel I/O needs a workbook path and the correct worksheet.
M20C04 · Explore · predict · practice
Query a small SQLite table
SQL retrieves data from tables using a query. This example creates a temporary in-memory database from the fictional data. The parameter placeholder passes a value separately from the query text.
Read the example
with sqlite3.connect(":memory:") as connection:
students().to_sql("students", connection, index=False, if_exists="replace")
result = pd.read_sql_query(
"SELECT Student_ID, Name, Score FROM students WHERE Score >= ? ORDER BY Score DESC",
connection, params=(85,))
print(result)
Watch for this. Only use if_exists="replace" here on the temporary classroom table; on a real database it replaces the existing table.
Your turn
Use SQL to calculate mean Score and student count for each Campus. Store the returned DataFrame in result with columns Campus, Mean_Score, and Students, ordered by Campus.
For the code check, keep these result variable names: result. Different valid approaches are welcome.
Show a hint
Use GROUP BY Campus with AVG(Score) AS Mean_Score and COUNT(*) AS Students.
Show one worked solution
with sqlite3.connect(":memory:") as connection:
students().to_sql("students", connection, index=False, if_exists="replace")
result = pd.read_sql_query(
"SELECT Campus, AVG(Score) AS Mean_Score, COUNT(*) AS Students FROM students GROUP BY Campus ORDER BY Campus",
connection)
print(result.round(2))
This performs the grouping inside SQL and returns the two-row result as a DataFrame.
Compare your explanations
Read. The database exists in memory for this connection and is not a disk database file.
Predict. Fatima, S006, with Score 95.
Explain. It passes values safely without manually inserting them into SQL text.
One quick check
What does read_sql_query return?
What you should be able to explain. Pandas can receive SQL results without loading every source row.