Skip to main content

Create a New Column - Solution & Explanation

Easy5 min read
Practice this problem

Problem Statement

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.

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.

Code

Python

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.

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.

Code

Python

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 →

Approach 3: Direct Calculation

We can directly calculate the double of salary and then store the result in the bonus column.

The time complexity is O(1), and the space complexity is O(1).

Code

Python

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.

Direct Calculation—

Detailed 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

Video Solution

Create new column LeetCode #2881 • CuteLeetCrafter • 109 views views

Watch 5 more video 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.

Ready to solve this problem?

Practice Create a New Column with our built-in code editor and test cases.

Practice on FleetCode