
Sponsored
Sponsored
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.
Time Complexity: O(n), where n is the number of employees.
Space Complexity: O(1), only a constant space is used for temporary variables.
1import pandas as pd
2
3data = {'name': ['Jack', 'Piper'
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.
This approach leverages DataFrame vectorized operations, which are optimized and efficient for operations on entire columns without needing explicit loops.
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.
1import pandas as pd
2
3data = {'name': ['Jack', 'Piper', 'Mia', 'Ulysses'],
4 'salary': [19666, 74754, 62509, 54866]}
5employees = pd.DataFrame(data)
6
7# Use vectorized operation
8employees['salary'] *= 2
9
10print(employees)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.