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.
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.
Problem Overview: You are given a table-like dataset and need to update values in specific columns according to a rule. The goal is to modify those columns efficiently while keeping the rest of the data unchanged.
Approach 1: Loop and Update Method (O(n * m) time, O(1) extra space)
This approach iterates through each target column and updates its values row by row. You loop over the list of column names, access the column, and apply the transformation directly. The key idea is explicit iteration: read a value, compute the updated value, and write it back. Time complexity is O(n * m) where n is the number of rows and m is the number of columns being modified. Space complexity stays O(1) since the updates happen in place. This approach is straightforward and easy to debug, but Python-level loops can become slower on large datasets.
Approach 2: Vectorized Operations Method (O(n * m) time, O(1) extra space)
This method relies on vectorized operations provided by libraries like Pandas. Instead of iterating through rows manually, you select the target columns and apply the transformation to the entire column at once. Under the hood, the operation runs in optimized C-based routines, which makes it significantly faster than Python loops. The time complexity remains O(n * m), but the constant factors are much smaller due to vectorization. Space complexity stays O(1) if the update is applied in place.
Vectorized operations are common when working with tabular data structures and are widely used in data processing pipelines. Understanding when to replace explicit loops with vectorized expressions is a key performance optimization when working with Python data workflows or large array-like datasets.
Recommended for interviews: Start with the loop-based approach to demonstrate the core logic and correctness. Then move to the vectorized solution to show familiarity with optimized data operations. Interviewers generally expect the vectorized approach when working with tabular structures because it is cleaner, faster, and closer to real-world data processing patterns.
This approach uses a loop to iterate through each row of the DataFrame and manually updates the 'salary' column by multiplying it by 2. This method is straightforward and helps understand the manipulation of each data point.
In this Python solution, we use a for loop to iterate over the DataFrame rows using the iterrows() method. For each row, we update the 'salary' column by accessing the current index and multiplying the current value by 2. This changes the original DataFrame in place.
Python
Time Complexity: O(n), where n is the number of employees.
Space Complexity: O(1), only a constant space is used for temporary variables.
This approach leverages DataFrame vectorized operations, which are optimized and efficient for operations on entire columns without needing explicit loops.
In this Python solution, we utilize the DataFrame's ability to perform vectorized operations. Here, we directly multiply the entire 'salary' column by 2 using employees['salary'] *= 2. This operation is highly efficient and modifies the DataFrame in place.
Python
Time Complexity: O(n), where n is the number of employees. This is performed efficiently due to the vectorized operation.
Space Complexity: O(1), as modifications are done in place.
Python
| Approach | Complexity |
|---|---|
| Loop and Update Method | Time Complexity: O(n), where n is the number of employees. |
| Vectorized Operations Method | Time Complexity: O(n), where n is the number of employees. This is performed efficiently due to the vectorized operation. |
| Default Approach | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Loop and Update Method | O(n * m) | O(1) | Good for understanding the transformation logic or when working in environments without vectorized support |
| Vectorized Operations Method | O(n * m) | O(1) | Best for large datasets and production code using Pandas or similar columnar libraries |
2884. Modify Columns | LeetCode | Python | Pandas • You Data And AI • 601 views views
Watch 5 more video solutions →Practice Modify Columns with our built-in code editor and test cases.
Practice on FleetCode