I\'m trying to set multiple new columns to one column and, separately, multiple new columns to multiple scalar values. Can\'t do either. Any way to do it other than setting
Maybe it is what you are looking for.
df=pd.DataFrame(columns=['A','B'],data=np.arange(6).reshape(3,2))
df['C'], df['D'] = df['A'], df['A']
df['E'], df['F'] = 0, 1
# Result
A B C D E F
0 0 1 0 0 0 1
1 2 3 2 2 0 1
2 4 5 4 4 0 1
for c in ['C', 'D']:
df[c] = d['A']
df['C'] = 0
df['D'] = 1
The assign method will create multiple, new columns in one step. You can pass a dict()
with the column and values to return a new DataFrame with the new columns appended to the end.
Using your examples:
df = df.assign(**{'C': df['A'], 'D': df['A']})
and
df = df.assign(**{'C': 0, 'D':1})
See this answer for additional detail: https://stackoverflow.com/a/46587717/4843561