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

Question:

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
Asked By: arkisle

||

Answers:

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

Answered By: Thtu

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
Answered By: Johan Dettmar

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)
Answered By: Sergei Frolov
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.