Create a New Column - Problem
You are given a DataFrame employees with the following structure:
| Column Name | Type |
|---|---|
| name | object |
| salary | int |
A company plans to provide its employees with a bonus.
Write a solution to create a new column named bonus that contains the doubled values of the salary column.
Input & Output
Example 1 — Basic Employee Bonus
$
Input:
employees = [{"name": "Alice", "salary": 50000}, {"name": "Bob", "salary": 60000}]
›
Output:
[{"name": "Alice", "salary": 50000, "bonus": 100000}, {"name": "Bob", "salary": 60000, "bonus": 120000}]
💡 Note:
Alice's bonus = 50000 * 2 = 100000, Bob's bonus = 60000 * 2 = 120000
Example 2 — Single Employee
$
Input:
employees = [{"name": "Charlie", "salary": 75000}]
›
Output:
[{"name": "Charlie", "salary": 75000, "bonus": 150000}]
💡 Note:
Charlie's bonus = 75000 * 2 = 150000
Example 3 — Multiple Employees
$
Input:
employees = [{"name": "David", "salary": 45000}, {"name": "Eva", "salary": 55000}, {"name": "Frank", "salary": 65000}]
›
Output:
[{"name": "David", "salary": 45000, "bonus": 90000}, {"name": "Eva", "salary": 55000, "bonus": 110000}, {"name": "Frank", "salary": 65000, "bonus": 130000}]
💡 Note:
Each employee gets bonus = salary * 2: David: 90000, Eva: 110000, Frank: 130000
Constraints
- 1 ≤ employees.length ≤ 104
- 1 ≤ salary ≤ 106
- name contains only alphabetic characters
Visualization
Tap to expand
Understanding the Visualization
1
Input DataFrame
Original employees data with name and salary columns
2
Apply Formula
Calculate bonus = salary * 2 for each employee
3
Output DataFrame
Extended DataFrame with new bonus column
Key Takeaway
🎯 Key Insight: DataFrame vectorized operations allow efficient column-wide transformations in a single line of code
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code