How to Add an Empty Column to a Dataframe

How to add an empty column to a dataframe?

If I understand correctly, assignment should fill:

>>> import numpy as np
>>> import pandas as pd
>>> df = pd.DataFrame({"A": [1,2,3], "B": [2,3,4]})
>>> df
A B
0 1 2
1 2 3
2 3 4
>>> df["C"] = ""
>>> df["D"] = np.nan
>>> df
A B C D
0 1 2 NaN
1 2 3 NaN
2 3 4 NaN

Add multiple empty columns to pandas DataFrame

I'd concat using a DataFrame:

In [23]:
df = pd.DataFrame(columns=['A'])
df

Out[23]:
Empty DataFrame
Columns: [A]
Index: []

In [24]:
pd.concat([df,pd.DataFrame(columns=list('BCD'))])

Out[24]:
Empty DataFrame
Columns: [A, B, C, D]
Index: []

So by passing a list containing your original df, and a new one with the columns you wish to add, this will return a new df with the additional columns.


Caveat: See the discussion of performance in the other answers and/or the comment discussions. reindex may be preferable where performance is critical.

How to add columns to an empty pandas dataframe?

Here are few ways to add an empty column to an empty dataframe:

df=pd.DataFrame(columns=['a'])
df['b'] = None
df = df.assign(c=None)
df = df.assign(d=df['a'])
df['e'] = pd.Series(index=df.index)
df = pd.concat([df,pd.DataFrame(columns=list('f'))])
print(df)

Output:

Empty DataFrame
Columns: [a, b, c, d, e, f]
Index: []

I hope it helps.

add empty column to existing DataFrame and populate it with a for loop

So I found this solution:

df["new_col"] = np.nan
for index, row in df.iterrows():
n = row["col2"]-0.5 # example
df.at[index,"new_col"] = n

Adding an empty column after the final column of the dataframe

You can do something like:

df['new_col'] = ''

Python Pandas Add empty column in the middle of a dataframe

you should use

df.insert(loc, column, value)

with loc being the index and column the column name and value it's value

for an empty column

df.insert(loc=2, column='new col', value=['' for i in range(df.shape[0])])

Create empty columns with mutate, column names from a vector

It's probably not really in the spirit of the tidyverse, but you can do

df %>% mutate(`[<-`(df, sn, value = NA))
#> pseudonym sn01 sn02 sn03
#> 1 a NA NA NA
#> 2 b NA NA NA
#> 3 c NA NA NA
#> 4 d NA NA NA


Related Topics



Leave a reply



Submit