Problem

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.

Examples

Example 1

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

    
    
    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.

## Solution

### Method 1  Using pandas DataFrame Assignment

#### Intuition

The key idea is to use pandas' ability to assign a new column directly to a DataFrame. By multiplying the `salary` column by 2, we efficiently create the `bonus` column.

#### Approach

1. The function receives a DataFrame `employees` with columns `name` and `salary`.
2. Assign a new column `bonus` as `employees["salary"] * 2`.
3. Return the modified DataFrame.

#### Code

1
2
3
4
def createBonusColumn(employees: 'pd.DataFrame') -> 'pd.DataFrame':
    employees = employees.copy()
    employees["bonus"] = employees["salary"] * 2
    return employees
#### Complexity - Time complexity: `O(n)`, where n is the number of rows in employees, as each row is processed once. - 🧺 Space complexity: `O(n)`, for storing the new DataFrame.