How to Add a New Column to an Existing DataFrame with Default Value in Pandas
We can use the add_columns() assign()
and add_columns() insert()
methods of the DataFrame object to add new columns to an existing DataFrame with default values. We can also assign default values directly to the DataFrame columns to be created.
In the following sections, we will use the following DataFrame
as an example.
import pandas as pd
dates = ["April-10", "April-11", "April-12", "April-13"]
fruits = ["Apple", "Papaya", "Banana", "Mango"]
prices = [3, 1, 2, 4]
df = pd.DataFrame({"Date": dates, "Fruit": fruits, "Price": prices})
print(df)
Output:
Date Fruit Price
0 April-10 Apple 3
1 April-11 Papaya 1
2 April-12 Banana 2
3 April-13 Mango 4
pandas.DataFrame.assign()
Adding New Columns to a Pandas DataFrame
We can use pandas.DataFrame.assign() method to add new columns to an existing DataFrame and DataFrame
assign default values to the newly created columns.
import pandas as pd
dates = ["April-10", "April-11", "April-12", "April-13"]
fruits = ["Apple", "Papaya", "Banana", "Mango"]
prices = [3, 1, 2, 4]
df = pd.DataFrame({"Date": dates, "Fruit": fruits, "Price": prices})
new_df = df.assign(Profit=6)
print(new_df)
Output:
Date Fruit Price Profit
0 April-10 Apple 3 6
1 April-11 Papaya 1 6
2 April-12 Banana 2 6
3 April-13 Mango 4 6
This code creates a new column in the DataFrame Profit
and sets the value of the entire column to 6
.
Access the new column to set it to the default value
We can use DataFrame indexing to create new columns in a DataFrame and set them to default values.
grammar:
df[col_name] = value
It df
creates a new column in the DataFrame col_name
and sets the default value for the entire column value
.
import pandas as pd
dates = ["April-10", "April-11", "April-12", "April-13"]
fruits = ["Apple", "Papaya", "Banana", "Mango"]
prices = [3, 1, 2, 4]
df = pd.DataFrame({"Date": dates, "Fruit": fruits, "Price": prices})
df["Profit"] = 5
print(df)
Output:
Date Fruit Price Profit
0 April-10 Apple 3 5
1 April-11 Papaya 1 5
2 April-12 Banana 2 5
3 April-13 Mango 4 5
pandas.DataFrame.insert()
Adding New Columns to a Pandas DataFrame
pandas.DataFrame.insert() allows us to insert a column in a DataFrame at a specified position.
grammar:
DataFrame.insert(loc, column, value, allow_duplicates=False)
It loc
creates a column
new column named at position with a default value of value
. allow_duplicates=False
Make sure that there is only one column
column named in the dataFrame.
import pandas as pd
dates = ["April-10", "April-11", "April-12", "April-13"]
fruits = ["Apple", "Papaya", "Banana", "Mango"]
prices = [3, 1, 2, 4]
df = pd.DataFrame({"Date": dates, "Fruit": fruits, "Price": prices})
df.insert(2, "profit", 4, allow_duplicates=False)
print(df)
Output:
Date Fruit profit Price
0 April-10 Apple 4 3
1 April-11 Papaya 4 1
2 April-12 Banana 4 2
3 April-13 Mango 4 4
Here, profit
a column named is inserted into the index 2
with a default value of 4
.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Convert Tensor to NumPy array in Python
Publish Date:2025/05/03 Views:85 Category:Python
-
This tutorial will show you how to convert a Tensor to a NumPy array in Python. Use the function in Python Tensor.numpy() to convert a tensor to a NumPy array Eager Execution of TensorFlow library can be used to convert tensor to NumPy arra
Saving NumPy arrays as images in Python
Publish Date:2025/05/03 Views:193 Category:Python
-
In Python, numpy module is used to manipulate arrays. There are many modules available in Python that allow us to read and store images. An image can be thought of as an array of different pixels stored at specific locations with correspond
Transposing a 1D array in NumPy
Publish Date:2025/05/03 Views:98 Category:Python
-
Arrays and matrices form the core of this Python library. The transpose of these arrays and matrices plays a vital role in certain topics such as machine learning. In NumPy, it is easy to calculate the transpose of an array or a matrix. Tra
Find the first index of an element in a NumPy array
Publish Date:2025/05/03 Views:58 Category:Python
-
In this tutorial, we will discuss how to find the first index of an element in a numpy array. Use where() the function to find the first index of an element in a NumPy array The function in the numpy module where() is used to return an arra
Remove Nan values from NumPy array
Publish Date:2025/05/03 Views:118 Category:Python
-
This article discusses some built-in NumPy functions that you can use to remove nan values. Remove Nan values using logical_not() and methods in NumPy isnan() logical_not() is used to apply logical NOT to the elements of an array. isn
Normalizing a vector in Python
Publish Date:2025/05/03 Views:51 Category:Python
-
A common concept in the field of machine learning is to normalize a vector or dataset before passing it to the algorithm. When we talk about normalizing a vector, we say that its vector magnitude is 1, being a unit vector. In this tutorial,
Calculating Euclidean distance in Python
Publish Date:2025/05/03 Views:128 Category:Python
-
In the world of mathematics, the shortest distance between two points in any dimension is called the Euclidean distance. It is the square root of the sum of the squares of the differences between the two points. In Python, the numpy, scipy
Element-wise division in Python NumPy
Publish Date:2025/05/03 Views:199 Category:Python
-
This tutorial shows you how to perform element-wise division on NumPy arrays in Python. NumPy Element-Wise Division using numpy.divide() the function If we have two arrays and want to divide each element of the first array with each element
Convert 3D array to 2D array in Python
Publish Date:2025/05/03 Views:79 Category:Python
-
In this tutorial, we will discuss the methods to convert 3D array to 2D array in Python. numpy.reshape() Convert 3D array to 2D array using function in Python [ numpy.reshape() Function](numpy.reshape - NumPy v1.20 manual)Changes the shape