Modify Columns - Problem

You have a DataFrame employees with two columns:

Column NameType
nameobject
salaryint

A company intends to give its employees a pay rise. Write a solution to modify the salary column by multiplying each salary by 2.

The result should be the modified DataFrame with updated salaries.

Input & Output

Example 1 — Basic Employee DataFrame
$ Input: employees = [{"name": "Alice", "salary": 50000}, {"name": "Bob", "salary": 60000}]
Output: [{"name": "Alice", "salary": 100000}, {"name": "Bob", "salary": 120000}]
💡 Note: Alice's salary: 50000 × 2 = 100000, Bob's salary: 60000 × 2 = 120000
Example 2 — Single Employee
$ Input: employees = [{"name": "John", "salary": 75000}]
Output: [{"name": "John", "salary": 150000}]
💡 Note: John's salary doubled: 75000 × 2 = 150000
Example 3 — Multiple Employees
$ Input: employees = [{"name": "Emma", "salary": 40000}, {"name": "Mike", "salary": 80000}, {"name": "Sara", "salary": 55000}]
Output: [{"name": "Emma", "salary": 80000}, {"name": "Mike", "salary": 160000}, {"name": "Sara", "salary": 110000}]
💡 Note: All salaries doubled: Emma 40000→80000, Mike 80000→160000, Sara 55000→110000

Constraints

  • 1 ≤ employees.length ≤ 104
  • 1 ≤ salary ≤ 106
  • name consists of alphanumeric characters and spaces

Visualization

Tap to expand
Modify Columns - Vectorized Column Operation INPUT DataFrame: employees name salary Alice 50000 Bob 60000 Input Values: {"name":"Alice","salary":50000} {"name":"Bob","salary":60000} x2 Pay Rise Multiplier ALGORITHM STEPS 1 Access Column df['salary'] selects column 2 Vectorized Multiply df['salary'] * 2 3 Assign Back df['salary'] = result 4 Return DataFrame Modified in-place # Vectorized operation employees['salary'] = employees['salary'] * 2 50000 x2 = 100000 FINAL RESULT Modified DataFrame name salary Alice 100000 Bob 120000 Output Values: {"name":"Alice","salary":100000} {"name":"Bob","salary":120000} Salaries Doubled - OK All values multiplied by 2 Operation completed! Key Insight: Pandas vectorized operations apply to entire columns at once, making them extremely efficient. Instead of looping through each row, df['salary'] * 2 multiplies all values simultaneously using optimized NumPy arrays under the hood. This is the preferred method for DataFrame transformations. TutorialsPoint - Modify Columns | Vectorized Column Operation
Asked in
Google 15 Amazon 12 Microsoft 10 Facebook 8
28.6K Views
Medium Frequency
~5 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