I ran across this issue when trying to apply multiple scalar values to multiple new columns and couldn't find a better way. If I'm missing something blatantly obvious, let me know, but df[['b','c']] = 0
doesn't work. but here's the simplified code:
# Create the "current" dataframe
df = pd.DataFrame({'a':[1,2]})
# List of columns I want to add
col_list = ['b','c']
# Quickly create key : scalar value dictionary
scalar_dict = { c : 0 for c in col_list }
# Create the dataframe for those columns - key here is setting the index = df.index
df[col_list] = pd.DataFrame(scalar_dict, index = df.index)
Or, what appears to be slightly faster is to use .assign()
:
df = df.assign(**scalar_dict)