Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add multiple empty columns to pandas DataFrame

Tags:

python

pandas

How do I add multiple empty columns to a DataFrame from a list?

I can do:

    df["B"] = None     df["C"] = None     df["D"] = None 

But I can't do:

    df[["B", "C", "D"]] = None 

KeyError: "['B' 'C' 'D'] not in index"

like image 463
P A N Avatar asked Jun 18 '15 22:06

P A N


People also ask

How do I add multiple empty columns to a DataFrame in Python?

There are multiple ways to add a new empty/blank column (single or multiple columns) to a pandas DataFrame by using assign operator, assign() , insert() and apply() methods. By using these you can add one or multiple empty columns with either NaN , None , Blank or Empty string values to all cells.

How do I add a blank column to a DataFrame pandas?

Add Empty Column to Pandas You can add an empty column to the pandas dataframe using the = operator and assign null values to the column. What is this? An empty column will be added at the end of the dataframe with the column header Empty_Column. You can also add a column with nan values.

How do you create a blank DataFrame and add columns?

First, create an empty dataframe using pd. Dataframe() . Next, you can append a column to the created dataframe using the insert() method. To know more about other methods available to add columns to the dataframe, refer to add column to dataframe tutorial.


2 Answers

You could use df.reindex to add new columns:

In [18]: df = pd.DataFrame(np.random.randint(10, size=(5,1)), columns=['A'])  In [19]: df Out[19]:     A 0  4 1  7 2  0 3  7 4  6  In [20]: df.reindex(columns=list('ABCD')) Out[20]:     A   B   C   D 0  4 NaN NaN NaN 1  7 NaN NaN NaN 2  0 NaN NaN NaN 3  7 NaN NaN NaN 4  6 NaN NaN NaN 

reindex will return a new DataFrame, with columns appearing in the order they are listed:

In [31]: df.reindex(columns=list('DCBA')) Out[31]:      D   C   B  A 0 NaN NaN NaN  4 1 NaN NaN NaN  7 2 NaN NaN NaN  0 3 NaN NaN NaN  7 4 NaN NaN NaN  6 

The reindex method as a fill_value parameter as well:

In [22]: df.reindex(columns=list('ABCD'), fill_value=0) Out[22]:     A  B  C  D 0  4  0  0  0 1  7  0  0  0 2  0  0  0  0 3  7  0  0  0 4  6  0  0  0 
like image 91
unutbu Avatar answered Sep 28 '22 11:09

unutbu


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.

like image 21
EdChum Avatar answered Sep 28 '22 11:09

EdChum