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 NameType
nameobject
salaryint

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
DataFrame Bonus Column CreationStep 1: Original DataNameSalaryAlice50000Bob60000Charlie75000Step 2: Column Extractsalary column500006000075000ร— 2Step 3: Calculatebonus values100000120000150000Step 4: Final ResultNameSalaryBonusAlice50000100000Bob60000120000Charlie75000150000๐Ÿ’ก Key: Vectorized operations process entire columns simultaneously!Much faster than row-by-row processing
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

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

We create a new column to store n bonus values

n
2n
โšก 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
Asked in
Netflix 35 Spotify 28 Airbnb 22 Uber 18
34.5K Views
High Frequency
~8 min Avg. Time
892 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