【发布时间】:2017-04-20 18:25:39
【问题描述】:
我想将多个列添加到 pandas DataFrame 并将它们设置为等于现有列。有没有一种简单的方法可以做到这一点?在R 我会这样做:
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
在pandas 中,这导致KeyError: "['b' 'c'] not in index":
df = pd.DataFrame({'a': np.arange(1,6)})
df[['b','c']] = df.a
【问题讨论】: