Skip to main content
TopMiniSite

Back to all posts

How to Create A New Column In A Pandas DataFrame?

Published on
4 min read
How to Create A New Column In A Pandas DataFrame? image

Best Data Analysis Tools to Buy in November 2025

1 Python for Data Analysis: Data Wrangling with pandas, NumPy, and Jupyter

Python for Data Analysis: Data Wrangling with pandas, NumPy, and Jupyter

BUY & SAVE
$43.99 $79.99
Save 45%
Python for Data Analysis: Data Wrangling with pandas, NumPy, and Jupyter
2 Data Analysis with Open Source Tools: A Hands-On Guide for Programmers and Data Scientists

Data Analysis with Open Source Tools: A Hands-On Guide for Programmers and Data Scientists

BUY & SAVE
$14.01 $39.99
Save 65%
Data Analysis with Open Source Tools: A Hands-On Guide for Programmers and Data Scientists
3 Statistics: A Tool for Social Research and Data Analysis (MindTap Course List)

Statistics: A Tool for Social Research and Data Analysis (MindTap Course List)

BUY & SAVE
$81.77 $259.95
Save 69%
Statistics: A Tool for Social Research and Data Analysis (MindTap Course List)
4 Advanced Data Analytics with AWS: Explore Data Analysis Concepts in the Cloud to Gain Meaningful Insights and Build Robust Data Engineering Workflows Across Diverse Data Sources (English Edition)

Advanced Data Analytics with AWS: Explore Data Analysis Concepts in the Cloud to Gain Meaningful Insights and Build Robust Data Engineering Workflows Across Diverse Data Sources (English Edition)

BUY & SAVE
$29.95 $37.95
Save 21%
Advanced Data Analytics with AWS: Explore Data Analysis Concepts in the Cloud to Gain Meaningful Insights and Build Robust Data Engineering Workflows Across Diverse Data Sources (English Edition)
5 Data Analysis with LLMs: Text, tables, images and sound (In Action)

Data Analysis with LLMs: Text, tables, images and sound (In Action)

BUY & SAVE
$38.39
Data Analysis with LLMs: Text, tables, images and sound (In Action)
6 Head First Data Analysis: A learner's guide to big numbers, statistics, and good decisions

Head First Data Analysis: A learner's guide to big numbers, statistics, and good decisions

BUY & SAVE
$29.61 $59.99
Save 51%
Head First Data Analysis: A learner's guide to big numbers, statistics, and good decisions
7 Business Analytics: Data Analysis & Decision Making (MindTap Course List)

Business Analytics: Data Analysis & Decision Making (MindTap Course List)

BUY & SAVE
$68.44 $323.95
Save 79%
Business Analytics: Data Analysis & Decision Making (MindTap Course List)
8 Beyond the Basics: A Quick Guide to the Most Useful Excel Data Analysis Tools for the Business Analyst

Beyond the Basics: A Quick Guide to the Most Useful Excel Data Analysis Tools for the Business Analyst

BUY & SAVE
$6.99
Beyond the Basics: A Quick Guide to the Most Useful Excel Data Analysis Tools for the Business Analyst
+
ONE MORE?

To create a new column in a Pandas DataFrame, you can use either square brackets or the assign() method. Here are the two approaches:

  1. Using square brackets: You can directly assign values to a new column by specifying its name inside square brackets and setting it equal to the desired values. For example:

import pandas as pd

Create a DataFrame

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

Create a new column 'C' and assign values

df['C'] = [7, 8, 9]

This will add a new column named 'C' to the DataFrame df with the values [7, 8, 9].

  1. Using the assign() method: The assign() method allows you to create a new column and assign values in a more flexible manner. It returns a new DataFrame with the added column while leaving the original DataFrame unchanged. For example:

import pandas as pd

Create a DataFrame

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

Create a new column 'C' and assign values using assign()

df_new = df.assign(C=[7, 8, 9])

Here, the assign() method creates a new DataFrame df_new with the added column 'C'.

Both approaches provide you with the capability to create a new column in a Pandas DataFrame according to your desired values.

How to convert a column to a different data type in a Pandas DataFrame?

To convert a column to a different data type in a Pandas DataFrame, you can use the astype() function.

Here is an example where we convert the 'Price' column from float to integer:

import pandas as pd

Sample DataFrame

df = pd.DataFrame({'Item': ['A', 'B', 'C'], 'Price': [10.50, 20.75, 15.25]})

Convert 'Price' column to integer

df['Price'] = df['Price'].astype(int)

print(df)

Output:

Item Price 0 A 10 1 B 20 2 C 15

In this example, we use the astype(int) method to convert the 'Price' column to integers. Similarly, you can use other data types such as float, bool, datetime, etc., according to your needs.

How to transform values in a column and store the results in a new column?

To transform values in a column and store the results in a new column, you can follow these steps:

  1. Import the necessary libraries. Typically, you would need the pandas library for this task.

import pandas as pd

  1. Read the data into a pandas DataFrame. You can use the read_csv() function if your data is in a CSV file or other similar functions for different file types.

df = pd.read_csv('data.csv')

  1. Define a transformation function. Create a function that takes a value from a specific column as input and returns the transformed value.

def transform(value): # Perform the desired transformation transformed_value = # Your code here return transformed_value

  1. Apply the transformation function to the column. Use the apply() function to apply your transformation function to the desired column. Assign the result to a new column.

df['new_column'] = df['original_column'].apply(transform)

In the code above, replace 'original_column' with the name of the column you want to transform, and 'new_column' with the name you want to give to the new column that will store the transformed values.

  1. Inspect the DataFrame. Print or view the updated DataFrame to verify that the transformation has been applied correctly.

print(df)

That's it! The values in the specified column will be transformed using your function, and the results will be stored in a new column in the DataFrame.

How to copy a column to create a new one in a DataFrame?

To copy a column and create a new one in a DataFrame, you can use the copy() method along with the indexing operator [] to select the specific column you want to copy.

Here is an example:

import pandas as pd

Create a sample DataFrame

data = {'Name': ['John', 'Paul', 'George', 'Ringo'], 'Age': [22, 25, 28, 23]} df = pd.DataFrame(data)

Copy the 'Age' column to create a new 'Age_Copy' column

df['Age_Copy'] = df['Age'].copy()

Display the DataFrame

print(df)

Output:

Name  Age  Age\_Copy

0 John 22 22 1 Paul 25 25 2 George 28 28 3 Ringo 23 23

In the example above, the copy() method is used to create a separate copy of the 'Age' column. The new column is assigned to the DataFrame using the indexing operator [] and the column name.