Menu
Coddy logo textTech

Custom Modifications

Lesson 13 of 19 in Coddy's Pandas Analytics course.

To make a custom modification, use the .apply method and provide it with a lambda or a function. For example, to find the square of each number in a column:

df['num_squared'] = df['num'].apply(lambda x: x**2)

This can also be achieved by multiplying the same column by itself:

df['num_squared'] = df['num'] * df['num']

To add 2 to each row value:

df["add_two"] = df['num'].apply(lambda x: x+2)
df["add_two"] = df['num'] + 2

To substitute each value in a Series with another value, use the .map method and provide it with a function, a dictionary, or a Series.

For example, if we want to replace fruit names with numeric values:

fruits_to_num = {"apple": 1, "mango": 2, "grape": 3}
df["fruits"] = df["fruits"].map(fruits_to_num)

Both .map and .apply can accept functions:

df['num_squared'] = df['num'].apply(lambda x: x**2)
df['num_squared'] = df['num'].map(lambda x: x**2)

To learn more about their difference, you can read here.

 

challenge icon

Challenge

Easy

The CSV file stats.csv contains information about stats.

Here is the first 5 lines of the file:

ID,COUNTRY,COLOR,SKILL,SKILL_POINTS,UTILIZATION,IS_VALID,CATEGORY
1,France,Signal violet,marksmanship,14,0.1924,1,SHOW
2,Solomon Islands,Pearl violet,crocheting,4,0.6108,1,TREE
3,Germany,Bottle green,calligraphy,12,0.88646,0,SHOW
4,Mauritania,Fawn brown,paper cutting,9,0.058,1,JAPE
  • Substitute the CATEGORY values to: {"SHOW": 0, "TREE": 1, "JAPE": 2, "GHUP": 3, "PLQR": 4}.
  • Create a new column named SKILL_MASTERY. Populate this column with the following formula: Multiply the values in the columns SKILL_POINTS and UTILIZATION. If the result is greater than 5, divide it by 4; otherwise, divide it by 2. Finally, add the value in the column IS_VALID to the result.
  • sort the result in ascending order according to SKILL_MASTERY.

Store the result in the df variable.

Try it yourself

# pandas as pd is already imported
df = pd.read_csv("./stats.csv")
# Write your code below

All lessons in Pandas Analytics