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

2. Reading and inspecting data

2.5 Sort and count: sort_values and value_counts

Highlights & notes

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

    Bookmarks

    Bookmark a lesson to see it here.

      Two questions come up with every new dataset: "what are the biggest values?" and "how often does each value appear?"

      • df.sort_values("amount") sorts the rows, smallest first. Add ascending=False for biggest first.
      • df["city"].value_counts() counts how many rows each city has, most common first

      value_counts is the quickest way to get to know a column. One line tells you the categories and how lopsided they are.

      Must Know:

      sort_values returns a new sorted DataFrame. The original keeps its order unless you save the result: df = df.sort_values("amount").

      Biggest first

      Sorting keeps the old row labels.

      Python

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

      Output

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

      Quick check

      Which gives the smallest amount first?

      How many of each?

      Count a column, then total another.

      Python

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

      Output

      city
      Noida    2
      Delhi    1
      Pune     1
      Name: count, dtype: int64
      890

      Quick check

      A column holds A, B, A, A. What does value_counts() show first?

      Practice in pandas

      Sort df by amount, smallest first, and print the first 2 rows.

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

      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.sort_values("amount").head(2))

      Question 1 · Easy

      What does value_counts() do?

      Question 2 · Medium

      How do you sort with the biggest amount first?

      Question 3 · Hard

      You run df.sort_values("amount") on its own line and then print(df). What do you see?

      Key takeaways

      sort_values sorts rows by a column; ascending=False puts the biggest first. value_counts counts each different value. Both return new objects.

      Lesson 11 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
      PreviousSort and count: sort_values and value_counts
      Save your progress free for 1 month. Start free · Log in