JIT — Intro to Data Science · Python Lab · Problem 20 of 30

Building a table

Names, cities and marks in one object that keeps its rows together — and the difference between a column and a table with one column in it.

Three lists that belong together

You could keep names in one list, cities in another and marks in a third, and rely on position 2 meaning the same student in all three. Sort one of them and that promise is broken silently, taking every conclusion with it.

A DataFrame is a table: columns with names, rows that stay together, and one row per student.

New word — DataFrame pd.DataFrame(data) where data is a dictionary. Each key becomes a column heading; each list becomes that column's values. The lists must be the same length, because they are rows read across.
Printing one shows the table with a numbered column down the left. Those numbers are the index — pandas puts them there for you, and they are how a row is identified once the order changes.
New word — Series A single column, taken out on its own: students["marks"].
It keeps the index alongside the values, so a mark never loses track of which row it came from. Printed, it shows the index down the left and a footer saying what it is called and what kind of values it holds.
A DataFrame is a collection of Series that share one index. Almost everything you do to a whole table can also be done to one column, and the answers come back in the same shape.
Careful — one pair of brackets or two students["marks"] hands back a Series: one column, on its own.
students[["marks"]] hands back a DataFrame that happens to have one column in it.
They hold the same numbers and they are not the same kind of thing, which shows up the moment you ask either of them a question. The average of a Series is a number. The average of a DataFrame is a Series — one answer per column, even when there is only one column.
shape, and reading it the right way round students.shape gives a pair: rows first, columns second. Four students in three columns is (4, 3).
It is a fact about the table, not a job it performs, so it takes no brackets — the same rule as .shape on a NumPy array in problem 18.

What you are building

name city marks 0 Anjali Nagpur 78 1 Farhan Pune 65 2 Priya Nagpur 80 3 Rohit Mumbai 92 Shape: (4, 3) Rows: 4 Columns: 3 Column names: ['name', 'city', 'marks'] 0 78 1 65 2 80 3 92 Name: marks, dtype: int64 That column's average: 78.75
Coming later This table was typed out by hand. Real ones are read from a file, and the first thing you do with a file you did not create is find out what is actually in it — problem 21.

Build the programme

Your plan

The steps still to place

The whole programme is laid out below. Five pieces are missing. Two of them come back with a perfectly good number that counts the wrong thing, and one changes what kind of object you are holding without changing any of the numbers in it.

Leave nothing on "choose…". A wrong pick does not always cause an error — sometimes it just prints something you did not expect, which is the harder kind of mistake to spot.

Run and read

Here is the whole programme. The marks are yours to change, and the table grows or shrinks to match. Watch the shape.

Your programme

Output

Nothing has run yet.