arkisle
arkisle

Reputation: 223

Add multiple columns with zero values from a list to a Pandas data frame

Say I have a data frame

id col1 col2
1  1    foo
2  1    bar

And a list of column names

l = ['col3', 'col4', 'col5']

How do I add new columns to the data frame with zero as values?

id col1 col2 col3 col4 col5
1  1    foo     0    0    0
2  1    bar     0    0    0

Upvotes: 14

Views: 21783

Answers (3)

Sergei Frolov
Sergei Frolov

Reputation: 11

Actually, provided solutions with assign and df.loc are pretty slow. And PerformanceWarning appears

I would actually modify existing answer and use something like:

d = dict.fromkeys(l, 0)
temp_df = pd.DataFrame(d, index=df.index)

df = pd.concat([df, temp_df], axis=1)

Upvotes: 1

Johan Dettmar
Johan Dettmar

Reputation: 29456

The current accepted answer produced the following warning on my machine (using pandas=1.4.2):

PerformanceWarning: DataFrame is highly fragmented.  This is usually the result of calling `frame.insert` many times, which has poor performance.  Consider joining all columns at once using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.copy()`

I got rid of these warnings by assigning new columns like so instead:

df.loc[:, l] = 0

Upvotes: 2

Thtu
Thtu

Reputation: 2032

You could try direct assignment (assuming your dataframe is named df):

for col in l:
    df[col] = 0

Or use the DataFrame's assign method, which is a slightly cleaner way of doing it if l can contain a value, an array or any pandas Series constructor.

# create a dictionary of column names and the value you want
d = dict.fromkeys(l, 0)
df.assign(**d)

Pandas Documentation on the assign method : http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.assign.html

Upvotes: 22

Related Questions