2884 Modify Columns

Problem Statement

DataFrame employees

Column Name
Type

name

object

salary

int

A company intends to give its employees a pay rise.

Write a solution to modify the salary column by multiplying each salary by 2.

The result format is in the following example.

For the whole problem statement, please refer here.

Plans

  • Load the necessary library (pandas).

  • Define the function modifySalaryColumn that takes in a DataFrame.

  • Inside the function:

    • Access the salary column.

    • Double each salary (multiply by 2).

    • Return the modified DataFrame.

Solution

import pandas as pd

def modifySalaryColumn(employees: pd.DataFrame) -> pd.DataFrame:
    # Double the salary of all employees
    employees['salary'] *= 2

    # Return the modified DataFrame
    return employees

Explanation

  1. Import Pandas

    • We start by importing the Pandas library, which provides data structures and operations for manipulating numerical tables and time series.

  2. Define the Function

    • We define a function modifySalaryColumn that takes a single argument employees, which is a DataFrame containing employee data.

  3. Modifying the Salary Column

    • We access the salary column of the DataFrame employees using the bracket notation.

    • We multiply each salary by 2 to give the employees a pay rise.

  4. Return the Result

    • We return the modified DataFrame after doubling the salary of all employees.

Last updated