I want to add multiple columns to a pandas
DataFrame
and set them equal to an existing column. Is there a simple way of doing this? In R
I would do:
df <- data.frame(a=1:5)
df[c('b','c')] <- df$a
df
a b c
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
In pandas
this results in KeyError: "['b' 'c'] not in index"
:
df = pd.DataFrame({'a': np.arange(1,6)})
df[['b','c']] = df.a