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 withassign - Make a column from conditions with
np.whereandnp.select - Rename, drop and replace with
rename,drop,replaceandmap - Change values in a table with
.loc, and explain Copy-on-Write
Growing a column
Step through, then run the cells.
sales["revenue"] = sales["kilos"] * sales["price"]: a new column, worked out from two others.sales["revenue"] = ...adds a column tosalesitself. 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 leavessalesalone. Useful when you're exploring and don't want to change anything yet.np.wherefrom lesson 2.5 makes a column of one of two values.np.selecthandles 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={...})anddrop(columns=[...])change the column names. Both give a new table.
Renaming, dropping and replacing
Fixing values is half of cleaning. Run the cells.
replacewith a dictionary swaps each old value for a new one:Lipa CityandLIPAboth becomeLipa. Values not in the dictionary are left as they are.mapwith a dictionary looks up every value. Here each store gets a code. Values not in the dictionary becomeNaN; 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 meantreplace.
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 = sales[...]makes a filtered table, andlipa["kilos"] = ...changes it.lipanow has 36, 34 and 32.- But
salesstill has Lipa's 35. In pandas 3, every filter or selection behaves as a copy. Changinglipanever changessales. This rule is called Copy-on-Write. - Doing it in one line,
sales[mask]["kilos"] = 0, does nothing at all, and pandas says so with aChainedAssignmentErrorwarning. The first brackets make a copy; the second set changes the copy, which is thrown away at once. - The way that works: one
.locwith the rows and the column together.sales.loc[mask, "kilos"] = 0changessalesitself.
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
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.
- pandas: pandas.DataFrame.assign: new columns in a new table
- pandas: Copy-on-Write: the rule pandas 3 uses, and chained assignment
- pandas: What’s new in 3.0.0: Copy-on-Write on for everyone, and the new str type
- NumPy: numpy.select: the first matching condition wins
- pandas: pandas.Series.map: values not in the dictionary become NaN