Skip to content

2881. Create a New Column

DataFrame employees

Column NameType
nameobject
salaryint.

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 the salary column.
  • The result format is in the following example.

Example

Input:

DataFrame employees

namesalary
Piper4548
Grace28150
Georgia1103
Willow6593
Finn74576
Thomas24433

Output:

namesalarybonus
Piper45489096
Grace2815056300
Georgia11032206
Willow659313186
Finn74576149152
Thomas2443348866

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 called createBonusColumn that takes a DataFrame employees as input and returns a DataFrame.
  • employees['bonus'] = employees['salary'] * 2: Create a new column bonus in the DataFrame employees by doubling the values in the column salary.
  • return employees: Return the updated DataFrame employees.