Problem

DataFrame employees +————-+——–+ | Column Name | Type | +————-+——–+ | name | object | | salary | int | +————-+——–+

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 format is in the following example.

Examples

Example 1

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
Input: DataFrame employees
+---------+--------+
| name    | salary |
+---------+--------+
| Jack    | 19666  |
| Piper   | 74754  |
| Mia     | 62509  |
| Ulysses | 54866  |
+---------+--------+
Output: +---------+--------+
| name    | salary |
+---------+--------+
| Jack    | 39332  |
| Piper   | 149508 |
| Mia     | 125018 |
| Ulysses | 109732 |
+---------+--------+
Explanation: Every salary has been doubled.

## Solution

### Method 1 โ€“ Direct Column Update

#### Intuition
The simplest way to double every salary is to directly update the `salary` column by multiplying its value by 2. This can be done efficiently in SQL or pandas with a single statement.

#### Approach
1. Use an UPDATE statement in SQL to multiply the `salary` column by 2 for all rows.
2. In pandas, use vectorized assignment to update the column in one line.
3. The operation is performed in-place, so no extra space is needed except for the result.

#### Code

1
2
UPDATE employees
SET salary = salary * 2;
1
2
UPDATE employees
SET salary = salary * 2;
1
2
3
def modify_salary(employees: pd.DataFrame) -> pd.DataFrame:
    employees['salary'] = employees['salary'] * 2
    return employees
#### Complexity * โฐ Time complexity: `O(n)` โ€” Each row's salary is updated once. * ๐Ÿงบ Space complexity: `O(1)` โ€” The update is in-place, no extra space is used.