2881. Create a New Column
DataFrame employees
Column Name | Type |
---|---|
name | object |
salary | int. |
Instructions
- 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 thesalary
column. - The result format is in the following example.
Example
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.
Submissions
python
import pandas as pd
def createBonusColumn(employees: pd.DataFrame) -> pd.DataFrame:
employees['bonus'] = employees['salary'] * 2
return employees
Explanation
Python (Pandas)
Submitted by @noeyislearning
import pandas as pd
: Import the pandas library to work with DataFrames.def createBonusColumn(employees: pd.DataFrame) -> pd.DataFrame:
: Define a function calledcreateBonusColumn
that takes a DataFrameemployees
as input and returns a DataFrame.employees['bonus'] = employees['salary'] * 2
: Create a new columnbonus
in the DataFrameemployees
by doubling the values in the columnsalary
.return employees
: Return the updated DataFrameemployees
.