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

Create a New Column

Number: 3066

Difficulty: Easy

Paid? No

Companies: N/A


Problem Description

Given a DataFrame named employees with columns name and salary, create a new column called bonus that contains the doubled value of the salary for each employee.


Key Insights

  • The task involves simple column-wise arithmetic on a DataFrame.
  • It leverages vectorized operations available in many programming languages/libraries.
  • The operation does not require any additional data structures; just iterating over the existing rows.
  • Performance is efficient as each row is processed exactly once.

Space and Time Complexity

Time Complexity: O(n), where n is the number of rows in the DataFrame. Space Complexity: O(n), if a new DataFrame copy is created; however, if done in-place the extra space usage is O(1).


Solution

The solution is straightforward: iterate over the DataFrame and create a new column bonus where each value is the salary value multiplied by 2. In languages with built-in DataFrame support (like Python with Pandas), use vectorized operations to achieve this in one line. For other languages, simulate the DataFrame structure with collections/arrays and update each element accordingly.


Code Solutions

import pandas as pd

def create_bonus_column(employees: pd.DataFrame) -> pd.DataFrame:
    # Create a new column 'bonus' by doubling the 'salary' column values
    employees['bonus'] = employees['salary'] * 2
    # Return the updated DataFrame
    return employees

# Example usage:
# data = {'name': ['Piper', 'Grace', 'Georgia', 'Willow', 'Finn', 'Thomas'],
#         'salary': [4548, 28150, 1103, 6593, 74576, 24433]}
# df = pd.DataFrame(data)
# print(create_bonus_column(df))
← Back to All Questions