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

Difficulty: Easy


Problem Description

Create a new column named bonus in the employees DataFrame that contains the doubled values of the salary column.


Key Insights

  • The bonus column is derived directly from the salary column by multiplying each salary value by 2.
  • The operation is straightforward as it involves basic arithmetic on numeric data.
  • The new column should maintain the same order as the original DataFrame.

Space and Time Complexity

Time Complexity: O(n), where n is the number of rows in the DataFrame, because we iterate through each salary to compute the bonus.

Space Complexity: O(n), as we are creating a new column that holds the bonus values, which requires additional storage proportional to the number of employees.


Solution

To solve this problem, we will use a DataFrame structure that allows us to manipulate tabular data. The algorithmic approach involves iterating through the salary column and calculating each bonus value by multiplying the salary by 2. This will be implemented by adding a new column to the existing DataFrame.


Code Solutions

import pandas as pd

# Sample DataFrame creation
employees = pd.DataFrame({
    'name': ['Piper', 'Grace', 'Georgia', 'Willow', 'Finn', 'Thomas'],
    'salary': [4548, 28150, 1103, 6593, 74576, 24433]
})

# Creating the bonus column by doubling the salary
employees['bonus'] = employees['salary'] * 2

# Display the updated DataFrame
print(employees)
← Back to All Questions