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 a New Column - Vectorized Operation INPUT name salary Alice 50000 Bob 60000 Input Data: employees = [ {"name": "Alice", "salary": 50000}, {"name": "Bob", "salary": 60000}] Goal: Add bonus column bonus = salary * 2 ALGORITHM STEPS 1 Load DataFrame Read employees data 2 Access salary column df['salary'] 3 Vectorized multiply df['salary'] * 2 4 Assign to new column df['bonus'] = result df['bonus'] = df['salary'] * 2 [50000, 60000] * 2 = [100000, 120000] FINAL RESULT name salary bonus Alice 50000 100000 Bob 60000 120000 Output: [ {"name": "Alice", "salary": 50000, "bonus": 100000}, {"name": "Bob", "salary": 60000, "bonus": 120000}] OK - Complete! Key Insight: Vectorized operations in pandas apply calculations to entire columns at once, avoiding slow loops. df['bonus'] = df['salary'] * 2 multiplies ALL salary values simultaneously, creating the new column efficiently. TutorialsPoint - Create a New Column | Vectorized Column Operation Approach
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