We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Modify Columns

Number: 3067

Difficulty: Easy

Paid? No

Companies: N/A


Problem Description

Given a DataFrame or a collection of employees, each having a name and a salary, your task is to modify the salary column by doubling each employee's salary. This simulates giving all employees a pay rise.


Key Insights

  • The problem requires you to update an existing data column.
  • Each value in the salary column needs to be multiplied by 2.
  • The transformation needs to be applied efficiently to all rows.

Space and Time Complexity

Time Complexity: O(n), where n is the number of employees.
Space Complexity: O(1) if modifying the original structure in-place.


Solution

We approach the problem by iterating over each employee's record and updating the salary field by multiplying it by 2.
For Python, we utilize pandas DataFrame column operations which are vectorized for efficiency.
For JavaScript, we use the map function on an array of employee objects.
In C++ and Java, we iterate over a list or vector of employee structures/objects to update the salary values.
Each of these methods leverages in-place modification when possible, ensuring minimal extra space usage.


Code Solutions

import pandas as pd

# Function to modify the salary column by doubling its value
def modify_columns(employees: pd.DataFrame) -> pd.DataFrame:
    employees['salary'] = employees['salary'] * 2  # Double each salary in the DataFrame
    return employees

# Example usage:
# df = pd.DataFrame({'name': ['Jack', 'Piper', 'Mia', 'Ulysses'], 'salary': [19666, 74754, 62509, 54866]})
# print(modify_columns(df))
← Back to All Questions