📖 Scenario: You work in a small company that tracks employee departments and their status. Some department names are outdated and need to be updated to new names.
🎯 Goal: You will create a pandas DataFrame with employee data, set up a dictionary for replacing old department names with new ones, use the replace() method to update the DataFrame, and finally print the updated DataFrame.
📋 What You'll Learn
Create a pandas DataFrame with specific employee data
Create a dictionary mapping old department names to new names
Use the replace() method on the DataFrame to update department names
Print the updated DataFrame
💡 Why This Matters
🌍 Real World
Companies often need to update or clean data, such as renaming departments or correcting typos in employee records.
💼 Career
Data analysts and data scientists frequently use value substitution to prepare data for analysis and reporting.
Progress0 / 4 steps
1
Create the employee DataFrame
Import pandas as pd and create a DataFrame called df with these exact data: columns 'Employee', 'Department', and 'Status'. The rows should be: 'Alice', 'HR', 'Active', 'Bob', 'Sales', 'Inactive', 'Charlie', 'HR', 'Active', 'David', 'Marketing', 'Active', 'Eva', 'Sales', 'Inactive'.
Pandas
Hint
Use pd.DataFrame with a dictionary where keys are column names and values are lists of column data.
2
Create the replacement dictionary
Create a dictionary called replace_dict that maps the old department names to new ones: 'HR' to 'Human Resources' and 'Sales' to 'Sales & Marketing'.
Pandas
Hint
Use curly braces {} to create a dictionary with the exact key-value pairs.
3
Replace old department names with new ones
Use the replace() method on the 'Department' column of df with replace_dict to update the department names. Assign the result back to the 'Department' column of df.
Pandas
Hint
Use df['Department'].replace(replace_dict) and assign it back to df['Department'].
4
Print the updated DataFrame
Print the DataFrame df to see the updated department names.
Pandas
Hint
Use print(df) to display the DataFrame.
Practice
(1/5)
1. What does the replace() method do in pandas?
easy
A. It sorts the DataFrame based on column values.
B. It deletes rows containing specific values.
C. It adds new columns to the DataFrame.
D. It swaps specified values in a DataFrame or Series with new values.
Solution
Step 1: Understand the purpose of replace()
The replace() method is used to change specific values in pandas data structures.
Step 2: Compare with other options
The other options describe sorting the DataFrame, deleting rows, and adding columns, which are unrelated to value substitution.
Final Answer:
It swaps specified values in a DataFrame or Series with new values. -> Option D
Quick Check:
replace() = value substitution [OK]
Hint: Remember: replace() swaps old values with new ones [OK]
Common Mistakes:
Confusing replace() with drop() or sort_values()
Thinking replace() adds or removes rows
Assuming replace() changes column names
2. Which of the following is the correct syntax to replace all occurrences of 0 with 100 in a pandas Series s?
easy
A. s.replace([100], [0])
B. s.replace(100, 0)
C. s.replace(0, 100)
D. s.replace({100: 0})
Solution
Step 1: Identify the correct replace() syntax
The syntax s.replace(old_value, new_value) replaces all old_value with new_value.
Step 2: Check each option
s.replace(0, 100) correctly replaces 0 with 100. The other options reverse the values: s.replace(100, 0), s.replace([100], [0]), s.replace({100: 0}).
Final Answer:
s.replace(0, 100) -> Option C
Quick Check:
replace(old, new) = s.replace(0, 100) [OK]
Hint: Use s.replace(old_value, new_value) for single value swap [OK]
Step 1: Understand replace() with dictionary mapping
Using a dictionary with keys as old values and values as new values allows multiple replacements in one call.
Step 2: Check each option
df.replace({'A': 'Pass', 'B': 'Pass', 'C': 'Fail', 'D': 'Fail', 'F': 'Fail'}) correctly maps each grade to 'Pass' or 'Fail'. df.replace(['A', 'B'], ['Pass', 'Pass']).replace(['C', 'D', 'F'], ['Fail', 'Fail', 'Fail']) uses two replace calls, which works but is two steps, not one call. df.replace({'Pass': ['A', 'B'], 'Fail': ['C', 'D', 'F']}) reverses keys and values, which is invalid. df.replace({'A': 'Pass', 'B': 'Fail', 'C': 'Pass', 'D': 'Fail', 'F': 'Pass'}) mixes mappings incorrectly.