Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 14.6 — pandas: Series and DataFrames
Python · Example 1 of 4

Build a DataFrame and look at it

Add a fifth person and rerun. Print df.shape to confirm the row count. The same code works for a shop's orders, a list of students or a set of support tickets: one row per thing, one column per fact.

pandas gives Python tables: a DataFrame is a table with labelled columns and rows, and a Series is one column. You select with [], loc and iloc, filter with boolean masks and add columns by assignment.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 6 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Make a DataFrame with columns item and price for three items and print the item with the highest price.

02

From the register example, print the average salary of people older than 25.

03

Add a column price_with_tax that is price times 1.2, and print the frame.

04

Give every person from Pune a raise of 5000 in place, with a single loc assignment, and print the frame. Start from city = Pune, Delhi, Pune and salary = 52000.0, 71000.0, 64000.5.

Explain it without notes

01

What is the difference between a Series and a DataFrame?

02

When do you use loc and when iloc? What is the difference in slice behaviour?

03

How do you filter rows by two conditions? What common mistakes appear?

04

Why does df[mask]["col"] = 0 not do what you want, and what is the fix?

05

Two Series with different index labels are added and most results are NaN. Why, and how do you get a sensible answer?

Build a DataFrame and look at it
Sign in to run this example in your browser.

Expected output

   name  age   city   salary
0  Asha   23   Pune  52000.0
1   Ben   31  Delhi  71000.0
rows, columns: (4, 4)
columns: ['name', 'age', 'city', 'salary']
non-null counts: {'name': 4, 'age': 4, 'city': 4, 'salary': 4}
        age   salary
count   4.0      4.0
mean   31.5  69250.1
std     9.6  15903.3
min    23.0  52000.0
25%    26.0  61000.4
50%    29.0  67500.2
75%    34.5  75750.0
max    45.0  90000.0
city
Pune      2
Delhi     1
Mumbai    1
Name: count, dtype: int64