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

2. Reading and inspecting data

2.4 Filter rows with conditions

Highlights & notes

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

    Bookmarks

    Bookmark a lesson to see it here.

      "Show me only the big orders." In SQL you write WHERE amount > 200. In pandas you do it in two small moves: make a column of True and False, then use it to pick rows.

      • df["amount"] > 200 makes the True/False column. It is called a mask.
      • df[mask] keeps the rows where the mask is True

      Join conditions with & (and) and | (or), and put each condition in brackets. To test against a list, use .isin([...]).

      Must Know:

      Use & and |, not the words and and or. And never forget the brackets: (df["a"] > 1) & (df["b"] == 2).

      A mask, then the rows

      Two steps you will later write in one line.

      Python

      import pandas as pddf = pd.DataFrame({    "name": ["Asha", "Ravi", "Meera", "Kabir"],    "city": ["Noida", "Delhi", "Pune", "Noida"],    "amount": [250, 400, 150, 90],})mask = df["amount"] > 200print(mask)print(df[mask])

      Output

      0     True
      1     True
      2    False
      3    False
      Name: amount, dtype: bool
         name   city  amount
      0  Asha  Noida     250
      1  Ravi  Delhi     400

      Quick check

      The mask is True, False, True. How many rows does df[mask] return?

      Two conditions, and a list

      Brackets round each condition.

      Python

      import pandas as pddf = pd.DataFrame({    "name": ["Asha", "Ravi", "Meera", "Kabir"],    "city": ["Noida", "Delhi", "Pune", "Noida"],    "amount": [250, 400, 150, 90],})print(df[(df["amount"] > 100) & (df["city"] == "Noida")])print(df[df["city"].isin(["Delhi", "Pune"])])

      Output

         name   city  amount
      0  Asha  Noida     250
          name   city  amount
      1   Ravi  Delhi     400
      2  Meera   Pune     150

      Quick check

      Which line is written correctly?

      Practice in pandas

      Print the rows of df where amount is less than 200.

      Expected output
          name   city  amount
      2  Meera   Pune     150
      3  Kabir  Noida      90

      Reference solution
      import pandas as pd
      df = pd.DataFrame({
          "name": ["Asha", "Ravi", "Meera", "Kabir"],
          "city": ["Noida", "Delhi", "Pune", "Noida"],
          "amount": [250, 400, 150, 90],
      })
      print(df[df["amount"] < 200])

      Question 1 · Easy

      What is a mask?

      Question 2 · Medium

      Which symbol means "and" between two pandas conditions?

      Question 3 · Hard

      How do you keep the rows whose city is Delhi or Pune?

      Key takeaways

      A comparison makes a True/False mask and df[mask] keeps the True rows. Combine masks with & and |, each in brackets, and use isin for lists.

      Lesson 10 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
      PreviousFilter rows with conditions
      Save your progress free for 1 month. Start free · Log in