*50*

Occasionally you may want to add an empty column to a pandas DataFrame.

Fortunately this is fairly easy to do and this tutorial shows several examples of how to do so using the following pandas DataFrame:

import numpy as np import pandas as pd #create DataFrame df = pd.DataFrame({'points': [25, 12, 15, 14, 19], 'assists': [5, 7, 7, 9, 12], 'rebounds': [11, 8, 10, 6, 6]}) #view DataFrame df points assists rebounds 0 25 5 11 1 12 7 8 2 15 7 10 3 14 9 6 4 19 12 6

**Example 1: Add an Empty Column Using “”**

The first way to add an empty column is to use** **quotations as follows:

#add new column titled 'steals' df['steals'] = "" #view DataFrame df points assists rebounds steals 0 25 5 11 1 12 7 8 2 15 7 10 3 14 9 6 4 19 12 6

**Example 2: Add an Empty Column Using Numpy**

Another way to add an empty column is to use **np.nan **as follows:

#add new column titled 'steals' df['steals'] = np.nan #view DataFrame df points assists rebounds steals 0 25 5 11 NaN 1 12 7 8 NaN 2 15 7 10 NaN 3 14 9 6 NaN 4 19 12 6 NaN

**Example 3: Add an Empty Column Using Pandas Series**

Another way to add an empty column is to use **pd.Series() **as follows:

#add new column titled 'steals' df['steals'] = pd.Series() #view DataFrame df points assists rebounds steals 0 25 5 11 NaN 1 12 7 8 NaN 2 15 7 10 NaN 3 14 9 6 NaN 4 19 12 6 NaN

**Example 4: Add an Empty Column Using Pandas Insert**

Another way to add an empty column is to use the **insert() **function as follows:

#create DataFrame df = pd.DataFrame({'points': [25, 12, 15, 14, 19], 'assists': [5, 7, 7, 9, 12], 'rebounds': [11, 8, 10, 6, 6]}) #insert empty column titled 'steals' into index position 2 df.insert(2, "steals", np.nan) #view DataFrame df points assists steals rebounds 0 25 5 NaN 11 1 12 7 NaN 8 2 15 7 NaN 10 3 14 9 NaN 6 4 19 12 NaN 6

The nice part about this approach is that you can insert the empty column into any position you’d like in the DataFrame.

**Example 5: Add Multiple Empty Columns at Once**

To add multiple empty columns at once, you can use the **reindex() **function as follows:

#create DataFrame df = pd.DataFrame({'points': [25, 12, 15, 14, 19], 'assists': [5, 7, 7, 9, 12], 'rebounds': [11, 8, 10, 6, 6]}) #add empty columns titled 'empty1' and 'empty2' df = df.reindex(columns = df.columns.tolist() + ['empty1', 'empty2']) #view DataFrame df points assists rebounds empty1 empty2 0 25 5 11 NaN NaN 1 12 7 8 NaN NaN 2 15 7 10 NaN NaN 3 14 9 6 NaN NaN 4 19 12 6 NaN NaN

*You can find more Python tutorials here.*