DataFrame employees
+-------------+--------+
| Column Name | Type. |
+-------------+--------+
| name | object |
| salary | int. |
+-------------+--------+
A company plans to provide its employees with a bonus.
Write a solution to create a new column name bonus that contains the doubled values of the salary column.
The result format is in the following example.
Example 1:
Input: DataFrame employees +---------+--------+ | name | salary | +---------+--------+ | Piper | 4548 | | Grace | 28150 | | Georgia | 1103 | | Willow | 6593 | | Finn | 74576 | | Thomas | 24433 | +---------+--------+ Output: +---------+--------+--------+ | name | salary | bonus | +---------+--------+--------+ | Piper | 4548 | 9096 | | Grace | 28150 | 56300 | | Georgia | 1103 | 2206 | | Willow | 6593 | 13186 | | Finn | 74576 | 149152 | | Thomas | 24433 | 48866 | +---------+--------+--------+ Explanation: A new column bonus is created by doubling the value in the column salary.
We can use Pandas for this task, leveraging its ability to perform operations over complete columns. Specifically, we can utilize the vectorized operation in Pandas to create a new column bonus by simply doubling the values of the existing salary column.
This Python solution uses Pandas to create a new column by applying a mathematical operation on an existing column. The multiplication operator (*) is used between the salary column and the scalar value 2, which doubles each of the salary values directly and assigns the result to a new column bonus.
Time Complexity: O(n) - Where n is the number of rows in the DataFrame, as it processes each row once.
Space Complexity: O(n) - Requires additional space for the new bonus column.
In the absence of vectorized operations, we can achieve the task by iterating over each row of the DataFrame manually. However, this method is generally less efficient than vectorized operations.
This implementation does not use vectorized operations and loops over each row of the DataFrame using the DataFrame's iterrows() method. It calculates the double salary for each employee and stores it in a list, which is then added as a new column to the DataFrame.
Time Complexity: O(n) - It explicitly visits each row to calculate the bonus.
Space Complexity: O(n) - Holds the list of bonuses in memory before adding it as a new column.
| Approach | Complexity |
|---|---|
| Pandas Vectorized Operations | Time Complexity: O(n) - Where n is the number of rows in the DataFrame, as it processes each row once. |
| Looping through DataFrame Rows | Time Complexity: O(n) - It explicitly visits each row to calculate the bonus. |
Valid Sudoku - Amazon Interview Question - Leetcode 36 - Python • NeetCode • 391,872 views views
Watch 9 more video solutions →Practice Create a New Column with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor