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.
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.
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.
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.
| 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. |
How To Separate Data Columns wise / text to columns #shorts #excel #msexcel #exceltutorial #viral • Techmicrosoft • 748,786 views views
Watch 9 more video solutions →Practice Modify Columns with our built-in code editor and test cases.
Practice on FleetCode