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

1. Introduction to pandas

1.5 Make new columns from old ones

Highlights & notes

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

    Bookmarks

    Bookmark a lesson to see it here.

      In Excel you type a formula in the first cell and drag it down. In pandas you write the formula once for the whole column. No loop, no dragging.

      df["tax"] = df["amount"] * 0.18 multiplies every amount and stores the answers in a new column called tax. If the column already exists, it is replaced.

      It works with comparisons too: df["amount"] > 200 gives True or False for each row. Text columns have helpers under .str, such as .str.upper().

      Must Know:

      Do not loop over the rows to calculate a column. Column maths is shorter and many times faster, because pandas does the work in one go.

      A tax column

      One line calculates all four rows.

      Python

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

      Output

          name   city  amount   tax
      0   Asha  Noida     250  45.0
      1   Ravi  Delhi     400  72.0
      2  Meera   Pune     150  27.0
      3  Kabir  Noida      90  16.2

      Quick check

      df["double"] = df["amount"] * 2 does what?

      True or False, and text helpers

      A comparison and a string helper.

      Python

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

      Output

          name   city  amount    big code
      0   Asha  Noida     250   True  NOI
      1   Ravi  Delhi     400   True  DEL
      2  Meera   Pune     150  False  PUN
      3  Kabir  Noida      90  False  NOI

      Quick check

      What is in df["amount"] > 200 for the amounts 250, 400, 150, 90?

      Practice in pandas

      Add a column double that is amount times 2. Then print df.

      Expected output
          name   city  amount  double
      0   Asha  Noida     250     500
      1   Ravi  Delhi     400     800
      2  Meera   Pune     150     300
      3  Kabir  Noida      90     180

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

      Question 1 · Easy

      How do you add a column named tax?

      Question 2 · Medium

      What happens if you assign to a column name that already exists?

      Question 3 · Hard

      Why is column maths better than a loop over rows?

      Key takeaways

      Assign to df["new"] to add or replace a column. Maths and comparisons work on the whole column at once, and .str gives text helpers.

      Lesson 5 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
      PreviousMake new columns from old ones
      Save your progress free for 1 month. Start free · Log in