pandas basics · lesson 6 of 9
Parts in this lesson
Adf["revenue"] = df["kilos"] * df["price"]a new column, every row at once
Bnp.where(cond, "busy", "normal")2.5: one of two values
Cs.replace({"Lipa City": "Lipa"})fix values by a dictionary
Ddf.loc[mask, "kilos"] = 0change chosen cells in the table itself

New columns and changing values

Last updated

Analysis means adding to a table: revenue from kilos and price, a label for busy days, a store code. In this lesson you'll add, rename and drop columns, replace values, and learn the one rule pandas 3 uses to decide when changing one table changes another.

By the end of this lesson you can

  • Add a column worked out from other columns, with = and with assign
  • Make a column from conditions with np.where and np.select
  • Rename, drop and replace with rename, drop, replace and map
  • Change values in a table with .loc, and explain Copy-on-Write

Growing a column

Step through, then run the cells.

A new column filling row by row. sales["revenue"] = sales["kilos"] * sales["price"]: a new column, worked out from two others.4245.03546.02945.03845.03346.03044.0saleskilospricerevenue012345
1 of 5 sales["revenue"] = sales["kilos"] * sales["price"]: a new column, worked out from two others.
columns.ipynb
  • sales["revenue"] = ... adds a column to sales itself. The right side is Series maths, row by row and matched by index, as in lesson 3.1.
  • assign(vat=...) gives a new table with the extra column, and leaves sales alone. Useful when you're exploring and don't want to change anything yet.
  • np.where from lesson 2.5 makes a column of one of two values.
  • np.select handles more than two. It takes a list of conditions and a list of values, and the first condition that's true wins: under 31 is small, otherwise under 38 is medium, otherwise large. The order matters: put the narrowest condition first.
  • rename(columns={...}) and drop(columns=[...]) change the column names. Both give a new table.

Renaming, dropping and replacing

Fixing values is half of cleaning. Run the cells.

values.ipynb
  • replace with a dictionary swaps each old value for a new one: Lipa City and LIPA both become Lipa. Values not in the dictionary are left as they are.
  • map with a dictionary looks up every value. Here each store gets a code. Values not in the dictionary become NaN; if Tanauan were in this list, it would have no code. That's useful for catching values you didn't expect, and a trap if you meant replace.

Wrong way: changing a filtered table

You want to add 1 kg to each of Lipa's days, because its scale was reading light. Run the cells one at a time, reading each answer.

lipa.ipynb
  1. lipa = sales[...] makes a filtered table, and lipa["kilos"] = ... changes it. lipa now has 36, 34 and 32.
  2. But sales still has Lipa's 35. In pandas 3, every filter or selection behaves as a copy. Changing lipa never changes sales. This rule is called Copy-on-Write.
  3. Doing it in one line, sales[mask]["kilos"] = 0, does nothing at all, and pandas says so with a ChainedAssignmentError warning. The first brackets make a copy; the second set changes the copy, which is thrown away at once.
  4. The way that works: one .loc with the rows and the column together. sales.loc[mask, "kilos"] = 0 changes sales itself.

If you've used older pandas, or read older answers online, you'll have seen SettingWithCopyWarning. Before version 3, whether a filtered table shared memory with the original (like a NumPy view, lesson 2.6) depended on details you couldn't see, so changing one might change the other. Copy-on-Write ended the guessing. The rule to remember: to change a table, use .loc on that table.

Check what you learned

What’s the difference between df["vat"] = df["price"] * 0.12 and df.assign(vat=df["price"] * 0.12)?
s holds Batangas, Lipa and Rosario. What does s.map({"Batangas": "BAT", "Lipa": "LIP"}) give for Rosario?
You want to set Tanauan’s kilos to 0 in sales. Which works?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Data Analysis