Back to Chapters
Chapter Progress: 0 %0 / 6 Lessons Read
‹›

1. Introduction to pandas

1.3 DataFrame: a table of data

Highlights & notes

Select lesson text to highlight it, or tap Add note.

    Bookmarks

    Bookmark a lesson to see it here.

      A DataFrame is a table with named columns and labelled rows. The quickest way to build one is from a dictionary: each key is a column name and each list holds that column’s values.

      • df.shape → (rows, columns)
      • df.columns → the column names
      • df["city"] → one column, as a Series
      • len(df) → the number of rows

      In real projects you’ll usually read data from files, but building small tables yourself is the fastest way to learn and to test your code.

      A dictionary becomes a table

      • Keys → column names
      • Lists → column values
      • All lists must have the same length
      Must Know:

      Lists of different lengths raise ValueError: All arrays must be of the same length. Every column needs one value per row.

      How it fits together

      1. {"city": [...], "amount": [...]}
      2. pd.DataFrame(...)
      3. 3 rows × 2 columns

      Step through the code

      Python

      import pandas as pdorders = pd.DataFrame({    "city": ["Noida", "Delhi", "Pune"],    "amount": [250, 400, 150],})print(orders)print(orders.shape)print(orders["amount"].max())

      Output

          city  amount
      0  Noida     250
      1  Delhi     400
      2   Pune     150
      (3, 2)
      400

      Practice in pandas

      Build a DataFrame df with columns name (Asha, Ravi) and score (88, 92). Print its shape.

      Expected output
      (2, 2)

      Reference solution
      import pandas as pd
      df = pd.DataFrame({"name": ["Asha", "Ravi"], "score": [88, 92]})
      print(df.shape)

      Question 1 · Easy

      df has 4 rows and 3 columns. What is df.shape?

      Question 2 · Medium

      df = pd.DataFrame({"a": [1, 2], "b": [3, 4]}). What is df["b"].sum()?

      Question 3 · Hard

      What does pd.DataFrame({"a": [1, 2], "b": [3]}) do?

      Key takeaways

      A DataFrame is a table. Build one from a dictionary of equal-length lists, check its size with shape, and pick a column with df["name"].

      Lesson 3 of 13

      Save your progress

      Start free and your progress, code and notes stay saved. Free for 1 month, no card needed.

      Start free Log in
      PreviousDataFrame: a table of data
      Save your progress free for 1 month. Start free · Log in