We can use the drop method to remove columns from the dataset. This is not mutating unless we use the inplace parameter.
df.drop(columns=["aisle"], inplace=True)df.head()
id
name
dept
price
0
1
Chocolate Sandwich Cookies
snacks
3.50
1
2
All-Seasons Salt
pantry
4.99
2
3
Robust Golden Unsweetened Oolong Tea
beverages
2.49
3
4
Smart Ones Classic Favorites Mini Rigatoni Wit...
frozen
6.99
4
5
Green Chile Anytime Sauce
pantry
7.99
3.4 Creating New Columns
We can create a new column by specifying the name of that column and assigning a new value. For example, a column of constants:
df["inflation_factor"] =1.5df.head()
id
name
dept
price
inflation_factor
0
1
Chocolate Sandwich Cookies
snacks
3.50
1.5
1
2
All-Seasons Salt
pantry
4.99
1.5
2
3
Robust Golden Unsweetened Oolong Tea
beverages
2.49
1.5
3
4
Smart Ones Classic Favorites Mini Rigatoni Wit...
frozen
6.99
1.5
4
5
Green Chile Anytime Sauce
pantry
7.99
1.5
In practice, instead of creating a new column of constants or placeholder values, we will generally create a new column of transformed values (see “Mapping Columns” section below).
3.5 Mapping Columns
We can transform the values in one given column, and store the results in another column
When mapping using a single scalar value, for example multiplying all values in a column by 1.5:
When mapping using two columns, this performs an element-wise operation where the first values in each Series are compared, then the second values, etc.
In this example, we are using the department_abbrevs dictionary to look up the corresponding abbreviation for each department name in the “dept” column. As a result we obtain a new column of transformed values (the abbreviations), which we are storing back in the original DataFrame in a new column called “dept_abbrev”.
3.5.2 Applying a Transformation Function
For more complex mapping logic, we can use the apply method to apply a transformation function.
When defining our own transformation function, the function should accept a parameter representing one of the values in the column over which we are applying the function. It should operate on that input value and return the transformed value.
# TRANSFORMATION FUNCTION:def inflate_price(original_price):# only inflate if the price is greater than $5if original_price <5.00:return original_priceelse:return original_price *1.5assert inflate_price(3.00) ==3.00assert inflate_price(10.00) ==15.00
After defining the transformation function, we pass that function as a parameter to the apply method:
# APPLY TRANSFORMATION FUNCTION TO PRICE COL:df["inflated_price"] = df["price"].apply(inflate_price).round(2)df[["id", "name", "price", "inflated_price"]].head()
id
name
price
inflated_price
0
1
Chocolate Sandwich Cookies
3.50
3.50
1
2
All-Seasons Salt
4.99
4.99
2
3
Robust Golden Unsweetened Oolong Tea
2.49
2.49
3
4
Smart Ones Classic Favorites Mini Rigatoni Wit...
6.99
10.48
4
5
Green Chile Anytime Sauce
7.99
11.98
In this example, we are applying the transformation function inflate_price to each value in the “price” column. As a result we obtain a new column of transformed values, which we are storing back in the original DataFrame in a new column called “inflated_price”.