Difficulty: Easy | Acceptance: 90.10% | Paid: No Topics: N/A
Write a solution to create a new column named ‘bonus’ in the DataFrame employees. The values in this column should be double the values in the ‘salary’ column.
- Examples
- Constraints
- Approach 1: Direct Column Assignment
- Approach 2: Using assign Method
- Approach 3: Using apply Function
Examples
Example 1:
Input:
+---------+--------+
| name | salary |
+---------+--------+
| Piper | 10000 |
| Grace | 20000 |
| Georgia | 50000 |
+---------+--------+
Output:
+---------+--------+-------+
| name | salary | bonus |
+---------+--------+-------+
| Piper | 10000 | 20000 |
| Grace | 20000 | 40000 |
| Georgia | 50000 | 100000|
+---------+--------+-------+
Explanation: A new column ‘bonus’ is created by doubling the values in the ‘salary’ column.
Constraints
0 <= employees.length <= 100
1000 <= employees['salary'] <= 10^5
Approach 1: Direct Column Assignment
Intuition Pandas allows vectorized operations, meaning we can multiply an entire column (Series) by a scalar value directly without iterating through rows.
Steps
- Access the ‘salary’ column of the DataFrame.
- Multiply the Series by 2.
- Assign the result to a new column named ‘bonus’.
import pandas as pd
def createBonusColumn(employees: pd.DataFrame) -> pd.DataFrame:
employees['bonus'] = employees['salary'] * 2
return employeesComplexity
- Time: O(n) where n is the number of rows.
- Space: O(n) to store the new column.
- Notes: This is the most idiomatic and efficient way to perform column-wise arithmetic in Pandas.
Approach 2: Using assign Method
Intuition
The assign method returns a new object with all original columns in addition to new ones. It is useful for method chaining.
Steps
- Call the
assignmethod on the DataFrame. - Pass a keyword argument
bonusset toemployees['salary'] * 2. - Return the resulting DataFrame.
import pandas as pd
def createBonusColumn(employees: pd.DataFrame) -> pd.DataFrame:
return employees.assign(bonus=employees['salary'] * 2)Complexity
- Time: O(n) where n is the number of rows.
- Space: O(n) to store the new column (and potentially a copy of the DataFrame depending on implementation).
- Notes: This approach creates a new DataFrame copy, which can be safer for functional programming styles but uses slightly more memory than in-place assignment.
Approach 3: Using apply Function
Intuition
The apply function allows applying a custom function along an axis of the DataFrame. While slower than vectorization, it is flexible for complex logic.
Steps
- Select the ‘salary’ column.
- Use the
applymethod with a lambda function that multiplies the input by 2. - Assign the result to the ‘bonus’ column.
import pandas as pd
def createBonusColumn(employees: pd.DataFrame) -> pd.DataFrame:
employees['bonus'] = employees['salary'].apply(lambda x: x * 2)
return employeesComplexity
- Time: O(n) where n is the number of rows.
- Space: O(n) to store the new column.
- Notes: In Pandas,
applyis generally slower than vectorized operations because it often loops at the Python level (or Cython level with overhead). Use vectorization (Approach 1) for simple arithmetic.