Table of Contents
To add an empty column to a Pandas DataFrame, you can use the “assign” method and specify the name of the new column. This will create a new column with no values in it, which can then be filled in with desired data. Alternatively, you can use the “insert” method to insert an empty column at a specific index location. This can be useful when you want to add a column in between existing columns. Both methods allow for the addition of an empty column to a Pandas DataFrame without affecting the existing data.
Add Empty Column to Pandas DataFrame (3 Examples)
You can use the following methods to add empty columns to a pandas DataFrame:
Method 1: Add One Empty Column with Blanks
df['empty_column'] = ""
Method 2: Add One Empty Column with NaN Values
df['empty_column'] = np.nan
Method 3: Add Multiple Empty Columns with NaN Values
df[['empty1', 'empty2', 'empty3']] = np.nan
The following examples show how to use each method with the following pandas DataFrames:
import pandas as pd
#create DataFrame
df = pd.DataFrame({'team': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'],
'points': [18, 22, 19, 14, 14, 11, 20, 28],
'assists': [5, 7, 7, 9, 12, 9, 9, 4]})
#view DataFrame
print(df)
team points assists
0 A 18 5
1 B 22 7
2 C 19 7
3 D 14 9
4 E 14 12
5 F 11 9
6 G 20 9
7 H 28 4Example 1: Add One Empty Column with Blanks
The following code shows how to add one empty column with all blank values:
#add empty column
df['blanks'] = ""
#view updated DataFrame
print(df)
team points assists blanks
0 A 18 5
1 B 22 7
2 C 19 7
3 D 14 9
4 E 14 12
5 F 11 9
6 G 20 9
7 H 28 4 The new column called blanks is filled with blank values.
Example 2: Add One Empty Column with NaN Values
The following code shows how to add one empty column with all NaN values:
import numpy as np
#add empty column with NaN values
df['empty'] = np.nan#view updated DataFrame
print(df)
team points assists empty
0 A 18 5 NaN
1 B 22 7 NaN
2 C 19 7 NaN
3 D 14 9 NaN
4 E 14 12 NaN
5 F 11 9 NaN
6 G 20 9 NaN
7 H 28 4 NaNThe new column called empty is filled with NaN values.
Example 3: Add Multiple Empty Columns with NaN Values
The following code shows how to add multiple empty columns with all NaN values:
import numpy as np
#add three empty columns with NaN values
df[['empty1', 'empty2', 'empty3']] = np.nan
#view updated DataFrame
print(df)
team points assists empty1 empty2 empty3
0 A 18 5 NaN NaN NaN
1 B 22 7 NaN NaN NaN
2 C 19 7 NaN NaN NaN
3 D 14 9 NaN NaN NaN
4 E 14 12 NaN NaN NaN
5 F 11 9 NaN NaN NaN
6 G 20 9 NaN NaN NaN
7 H 28 4 NaN NaN NaNNotice that all three of the new columns are filled with NaN values.
The following tutorials explain how to perform other common tasks in pandas:
Cite this article
stats writer (2024). How can I add an empty column to a Pandas DataFrame?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-add-an-empty-column-to-a-pandas-dataframe/
stats writer. "How can I add an empty column to a Pandas DataFrame?." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-add-an-empty-column-to-a-pandas-dataframe/.
stats writer. "How can I add an empty column to a Pandas DataFrame?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-add-an-empty-column-to-a-pandas-dataframe/.
stats writer (2024) 'How can I add an empty column to a Pandas DataFrame?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-add-an-empty-column-to-a-pandas-dataframe/.
[1] stats writer, "How can I add an empty column to a Pandas DataFrame?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I add an empty column to a Pandas DataFrame?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
