Talentd
FleetCode
Make and follow sheets.Sign in

Create a New Column solution and explanation

Easy5 min read90.1% acceptanceUpdated 9 Apr 2026
Create new column LeetCode #2881CuteLeetCrafter · 1:38 · 109 viewsWatch on YouTube
5 more videos on this problem
  1. Create new column LeetCode #2881CuteLeetCrafter · 1:38 · 109 views
  2. 5. Create a New Column LeetCodeJohn Sharon Kumar J · 3:24 · 62 views
  3. Leetcode 2881 - Create new column [Pandas]Ptolémé · 1:15 · 18 views
  4. 2881. Create a New Column | LeetCode Using Python (Panda Library)Muhammad Marenah · 3:03 · 12 views
  5. 2881. Create a New Column | LeetCode | Data Science | Python | Data Analysis | TamilMastering AI · 5:16 · 10 views
  6. 2881 Create a new Column LeetCodeBrain Bloom · 0:32 · 3 views

The problem

DataFrame employees
+-------------+--------+
| Column Name | Type.  |
+-------------+--------+
| name        | object |
| salary      | int.   |
+-------------+--------+

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 1:

Input:
DataFrame employees
+---------+--------+
| name    | salary |
+---------+--------+
| Piper   | 4548   |
| Grace   | 28150  |
| Georgia | 1103   |
| Willow  | 6593   |
| Finn    | 74576  |
| Thomas  | 24433  |
+---------+--------+
Output:
+---------+--------+--------+
| name    | salary | bonus  |
+---------+--------+--------+
| Piper   | 4548   | 9096   |
| Grace   | 28150  | 56300  |
| Georgia | 1103   | 2206   |
| Willow  | 6593   | 13186  |
| Finn    | 74576  | 149152 |
| Thomas  | 24433  | 48866  |
+---------+--------+--------+
Explanation: 
A new column bonus is created by doubling the value in the column salary.

Approach overview

Problem Overview: You receive a Pandas DataFrame representing employee data. The task is simple: create a new column called bonus where each value is double the employee's salary. The result should return the updated DataFrame with the new column added.

Approach 1: Looping Through DataFrame Rows (O(n) time, O(n) space)

A straightforward approach iterates through each row of the DataFrame, computes the bonus for that row, and stores the result in a new column. In Python, this is often done using iterrows() or a similar row-wise loop. For every row, you read the salary value, multiply it by 2, and assign the result to the corresponding index in the bonus column. This approach works for beginners because it mirrors how you would process a list: read one element, compute, store, repeat.

The downside is performance. Row-wise iteration in Pandas bypasses many internal optimizations and runs slower for large datasets. The time complexity is O(n) because every row is processed once, and the extra column requires O(n) additional space. While correct, this approach is generally avoided in production data workflows. If you're exploring row manipulation concepts in Python or learning DataFrame basics, it still helps build intuition.

Approach 2: Pandas Vectorized Operations (O(n) time, O(n) space)

The optimal approach uses Pandas vectorized operations. Instead of iterating row by row, you operate on the entire column at once. Pandas internally applies the computation using optimized C-backed routines, which makes it significantly faster and more concise.

You directly assign a new column using an expression such as df['bonus'] = df['salary'] * 2. The multiplication runs across the entire column in a single vectorized operation. Conceptually, Pandas broadcasts the arithmetic operation across every element in the salary Series and constructs the new bonus column automatically.

This method still touches each row once, so the time complexity remains O(n), but the implementation is cleaner and leverages Pandas' optimized execution engine. Space complexity is O(n) for storing the new column. Vectorized operations are a core concept in Pandas and general DataFrame manipulation.

Recommended for interviews: Interviewers expect the vectorized Pandas solution. The row-iteration approach demonstrates basic understanding but signals inefficient use of the library. Writing the vectorized expression immediately shows familiarity with real-world data processing patterns and Pandas best practices.

Approach 1: Pandas Vectorized Operations

We can use Pandas for this task, leveraging its ability to perform operations over complete columns. Specifically, we can utilize the vectorized operation in Pandas to create a new column bonus by simply doubling the values of the existing salary column.

import pandas as pd

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

# Creating the 'bonus' column with doubled salary values
employees['bonus'] = employees['salary'] * 2

print(employees)

This Python solution uses Pandas to create a new column by applying a mathematical operation on an existing column. The multiplication operator (*) is used between the salary column and the scalar value 2, which doubles each of the salary values directly and assigns the result to a new column bonus.

Complexity

Time Complexity: O(n) - Where n is the number of rows in the DataFrame, as it processes each row once.
Space Complexity: O(n) - Requires additional space for the new bonus column.

Try this approach in the editor

Approach 2: Looping through DataFrame Rows

In the absence of vectorized operations, we can achieve the task by iterating over each row of the DataFrame manually. However, this method is generally less efficient than vectorized operations.

import pandas as pd

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

# Initialize a list to store bonus values
bonuses = []

# Iterate over DataFrame rows using DataFrame.iterrows()
for index, row in employees.iterrows():
    # Calculate the bonus
    bonus = row['salary'] * 2
    # Append to the bonuses list
    bonuses.append(bonus)

# Assign the bonuses list to a new column in the DataFrame
employees['bonus'] = bonuses

print(employees)

This implementation does not use vectorized operations and loops over each row of the DataFrame using the DataFrame's iterrows() method. It calculates the double salary for each employee and stores it in a list, which is then added as a new column to the DataFrame.

Complexity

Time Complexity: O(n) - It explicitly visits each row to calculate the bonus.
Space Complexity: O(n) - Holds the list of bonuses in memory before adding it as a new column.

Try this approach in the editor

Complexity comparison

ApproachComplexity
Pandas Vectorized Operations

Time Complexity: O(n) - Where n is the number of rows in the DataFrame, as it processes each row once.
Space Complexity: O(n) - Requires additional space for the new bonus column.

Looping through DataFrame Rows

Time Complexity: O(n) - It explicitly visits each row to calculate the bonus.
Space Complexity: O(n) - Holds the list of bonuses in memory before adding it as a new column.

Complexity analysis

ApproachTimeSpaceWhen to Use
Looping Through DataFrame RowsO(n)O(n)When learning DataFrame iteration or debugging row-level logic
Pandas Vectorized OperationsO(n)O(n)Preferred for production Pandas workflows and interview solutions

Frequently asked questions

Create a New Column Python solution

In Python with Pandas, create the column using a vectorized assignment: df['bonus'] = df['salary'] * 2. This multiplies every salary value by two and stores the result in a new column called bonus while keeping the rest of the DataFrame unchanged.

Is Create a New Column easy or hard?

Create a New Column is classified as an Easy problem. The main concept is basic DataFrame manipulation in Pandas, and the optimal solution is a single vectorized column operation with O(n) time complexity.

How to solve Create a New Column in O(n)?

Use a vectorized column operation in Pandas. Multiply the existing salary column by 2 and assign it to a new column: df['bonus'] = df['salary'] * 2. The operation runs across the entire column and processes each element exactly once, resulting in O(n) time.

What is the best approach for Create a New Column?

The best approach uses Pandas vectorized operations. Assign the new column directly with an expression such as df['bonus'] = df['salary'] * 2. This processes the entire column in one operation with O(n) time complexity and leverages Pandas' optimized internal implementation.

Is Create a New Column asked at Google/Amazon/Meta?

This exact problem is more common in Python and data-processing interview rounds rather than traditional algorithm interviews. Similar tasks appear in data engineering, analytics, and machine learning roles where candidates manipulate Pandas DataFrames.

What data structure is used in Create a New Column?

The problem uses a Pandas DataFrame, which is a two-dimensional labeled data structure similar to a table. Each column is internally represented as a Pandas Series, enabling vectorized arithmetic operations across all rows.

What is the time complexity of Create a New Column?

The time complexity is O(n) because every row in the DataFrame must be processed once to compute the new column value. The space complexity is also O(n) since a new column with n values is added to the DataFrame.

Comments

Now write it yourselfClose this page and solve Create a New Column in the editor, without looking.
Practise Create a New Column