Modify Columns
EasyUpdated: Aug 2, 2025
Practice on:
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
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
{{< code_tabs >}}
##### MySQL
```sql
UPDATE employees
SET salary = salary * 2;
PostgreSQL
UPDATE employees
SET salary = salary * 2;
Python (pandas)
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.