How to Create an Empty Column in a Pandas DataFrame
We can add an empty column to a DataFrame in Pandas using the reindex()
, , assign()
and insert()
methods of the DataFrame object. We can also directly assign a null value to the column of the DataFrame to create an empty column in Pandas.
Creating Empty Pandas Columns with Simple Assignment
We can directly assign a column of DataFrame to an empty string, NaN
value or empty Pandas Series
to create an empty column in Pandas.
import pandas as pd
import numpy as np
dates = ["April-20", "April-21", "April-22", "April-23", "April-24", "April-25"]
income = [10, 20, 10, 15, 10, 12]
expenses = [3, 8, 4, 5, 6, 10]
df = pd.DataFrame({"Date": dates, "Income": income, "Expenses": expenses})
df["Empty_1"] = ""
df["Empty_2"] = np.nan
df["Empty_3"] = pd.Series()
print(df)
Output:
Date Income Expenses Empty_1 Empty_2 Empty_3
0 April-20 10 3 NaN NaN
1 April-21 20 8 NaN NaN
2 April-22 10 4 NaN NaN
3 April-23 15 5 NaN NaN
4 April-24 10 6 NaN NaN
5 April-25 12 10 NaN NaN
It creates three empty columns in df. Empty_1
The column is assigned an empty string, is Empty_2
assigned a NaN value, and is Empty_3
assigned an empty Pandas Series
which will also cause the entire Empty_3
to be NaN.
pandas.DataFrame.reindex()
Adding an empty column in Pandas using the
We can add multiple empty columns to the DataFrame in Pandas using pandas.DataFrame.reindex() method.
import pandas as pd
import numpy as np
dates = ["April-20", "April-21", "April-22", "April-23", "April-24", "April-25"]
income = [10, 20, 10, 15, 10, 12]
expenses = [3, 8, 4, 5, 6, 10]
df = pd.DataFrame({"Date": dates, "Income": income, "Expenses": expenses})
column_names = ["Empty_1", "Empty_2", "Empty_3"]
df = df.reindex(columns=column_names)
print(df)
Output:
Empty_1 Empty_2 Empty_3
0 NaN NaN NaN
1 NaN NaN NaN
2 NaN NaN NaN
3 NaN NaN NaN
4 NaN NaN NaN
5 NaN NaN NaN
Empty_1
This code creates new columns , Empty_2
, , with all NaN values in df, Empty_3
and all the old information is lost.
To add multiple new columns while preserving the initial columns, we can write code like this:
import pandas as pd
import numpy as np
dates = ["April-20", "April-21", "April-22", "April-23", "April-24", "April-25"]
income = [10, 20, 10, 15, 10, 12]
expenses = [3, 8, 4, 5, 6, 10]
df = pd.DataFrame({"Date": dates, "Income": income, "Expenses": expenses})
df = df.reindex(columns=df.columns.tolist() + ["Empty_1", "Empty_2", "Empty_3"])
print(df)
Output:
Date Income Expenses Empty_1 Empty_2 Empty_3
0 April-20 10 3 NaN NaN NaN
1 April-21 20 8 NaN NaN NaN
2 April-22 10 4 NaN NaN NaN
3 April-23 15 5 NaN NaN NaN
4 April-24 10 6 NaN NaN NaN
5 April-25 12 10 NaN NaN NaN
Empty_1
This will add empty columns , Empty_2
and, to df while preserving the initial information Empty_3
.
pandas.DataFrame.assign()
Adding an Empty Column to a Pandas DataFrame
We can add an empty column to the DataFrame in Pandas using pandas.DataFrame.assign() method.
import pandas as pd
import numpy as np
dates = ["April-20", "April-21", "April-22", "April-23", "April-24", "April-25"]
income = [10, 20, 10, 15, 10, 12]
expenses = [3, 8, 4, 5, 6, 10]
df = pd.DataFrame({"Date": dates, "Income": income, "Expenses": expenses})
df = df.assign(Empty_1="", Empty_2=np.nan)
print(df)
Output:
Date Income Expenses Empty_1 Empty_2
0 April-20 10 3 NaN
1 April-21 20 8 NaN
2 April-22 10 4 NaN
3 April-23 15 5 NaN
4 April-24 10 6 NaN
5 April-25 12 10 NaN
It will create an empty column named and containing only Empty_1
NaN values in .Empty_2
df
pandas.DataFrame.insert()
Adding empty columns to a DataFrame
pandas.DataFrame.insert() allows us to insert a column into a DataFrame at a specified position. We can use this method to DataFrame
add an empty column to .
grammar:
DataFrame.insert(loc, column, value, allow_duplicates=False)
It creates a new column loc
with name at position with default value . It ensures that there is only one column with name in . We can add an empty column to DataFrame if we pass an empty string or value as value argument.column
value
allow_duplicates = False
DataFrame
column
NaN
import pandas as pd
import numpy as np
dates = ["April-20", "April-21", "April-22", "April-23", "April-24", "April-25"]
income = [10, 20, 10, 15, 10, 12]
expenses = [3, 8, 4, 5, 6, 10]
df = pd.DataFrame({"Date": dates, "Income": income, "Expenses": expenses})
df.insert(3, "Empty_1", "")
df.insert(4, "Empty_2", np.nan)
print(df)
Output:
Date Income Expenses Empty_1 Empty_2
0 April-20 10 3 NaN
1 April-21 20 8 NaN
2 April-22 10 4 NaN
3 April-23 15 5 NaN
4 April-24 10 6 NaN
5 April-25 12 10 NaN
It df
creates Empty_1
a column in 3
with all null values at index and 4
creates a with all NaN
values at index Empty_2
.
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