Create a New Column - Problem

You are given a DataFrame employees with the following structure:

Column NameType
nameobject
salaryint

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
Create New Column: Add Bonus Column to Employee DataFrameNameSalaryAlice50000Bob60000Original DataFrameAdd Columnbonus = salary × 2NameSalaryBonusAlice50000100000Bob60000120000Enhanced DataFrameTransform: employees['bonus'] = employees['salary'] * 2Vectorized operation applies to all rows simultaneouslyResult: DataFrame with new bonus column containing doubled salary values
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
Asked in
Netflix 15 Airbnb 12
17.6K Views
Medium Frequency
~5 min Avg. Time
450 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen