Create a New Column - Problem
๐ข Employee Bonus Calculator
A company wants to reward its hardworking employees with a special bonus! You're given a DataFrame containing employee information with their names and current salaries.
Your task: Create a new column called bonus that contains double the salary for each employee.
Input Format:
You'll receive a pandas DataFrame employees with the following structure:
| Column Name | Type |
|---|---|
| name | object |
| salary | int |
Output:
Return the same DataFrame with an additional bonus column where each bonus equals 2 ร salary.
Example: If Alice earns $50,000, her bonus should be $100,000!
Input & Output
example_1.py โ Basic Employee Bonus
$
Input:
employees = pd.DataFrame({
'name': ['Alice', 'Bob'],
'salary': [50000, 60000]
})
โบ
Output:
name salary bonus
0 Alice 50000 100000
1 Bob 60000 120000
๐ก Note:
Alice earns $50,000 so her bonus is $50,000 ร 2 = $100,000. Bob earns $60,000 so his bonus is $60,000 ร 2 = $120,000.
example_2.py โ Multiple Employees
$
Input:
employees = pd.DataFrame({
'name': ['Charlie', 'Diana', 'Eve'],
'salary': [75000, 45000, 90000]
})
โบ
Output:
name salary bonus
0 Charlie 75000 150000
1 Diana 45000 90000
2 Eve 90000 180000
๐ก Note:
Each employee's bonus is calculated as double their salary: Charlie gets $150,000, Diana gets $90,000, and Eve gets $180,000.
example_3.py โ Single Employee Edge Case
$
Input:
employees = pd.DataFrame({
'name': ['John'],
'salary': [100000]
})
โบ
Output:
name salary bonus
0 John 100000 200000
๐ก Note:
Even with just one employee, the same logic applies: John's bonus of $200,000 is double his $100,000 salary.
Visualization
Tap to expand
Understanding the Visualization
1
Original DataFrame
Start with employee data containing names and salaries
2
Column Selection
Select the salary column for transformation
3
Vectorized Operation
Apply multiplication by 2 to all values simultaneously
4
New Column Creation
Assign the results to a new 'bonus' column
Key Takeaway
๐ฏ Key Insight: Pandas vectorized operations allow us to transform entire columns with mathematical operations in a single, efficient step - just like applying formulas in spreadsheets!
Time & Space Complexity
Time Complexity
O(n)
Vectorized operations still process n elements, but with much lower constant factors due to optimized C implementation
โ Linear Growth
Space Complexity
O(n)
We create a new column to store n bonus values
โก Linearithmic Space
Constraints
- 1 โค employees.shape[0] โค 104
- 1 โค employees['salary'][i] โค 106
- All salary values are positive integers
- Employee names are non-empty strings
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code