Reputation: 5660
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
Upvotes: 7
Views: 5812
Reputation: 210882
you can use .assign() method:
In [31]: df.assign(b=df['a'], c=df['a'])
Out[31]:
a b c
0 1 1 1
1 2 2 2
2 3 3 3
3 4 4 4
4 5 5 5
or a little bit more creative approach:
In [41]: cols = list('bcdefg')
In [42]: df.assign(**{col:df['a'] for col in cols})
Out[42]:
a b c d e f g
0 1 1 1 1 1 1 1
1 2 2 2 2 2 2 2
2 3 3 3 3 3 3 3
3 4 4 4 4 4 4 4
4 5 5 5 5 5 5 5
another solution:
In [60]: pd.DataFrame(np.repeat(df.values, len(cols)+1, axis=1), columns=['a']+cols)
Out[60]:
a b c d e f g
0 1 1 1 1 1 1 1
1 2 2 2 2 2 2 2
2 3 3 3 3 3 3 3
3 4 4 4 4 4 4 4
4 5 5 5 5 5 5 5
NOTE: as @Cpt_Jauchefuerst mentioned in the comment DataFrame.assign(z=1, a=1)
will add columns in alphabetical order - i.e. first a
will be added to existing columns and then z
.
Upvotes: 6
Reputation: 294488
A pd.concat
approach
df = pd.DataFrame(dict(a=range5))
pd.concat([df.a] * 5, axis=1, keys=list('abcde'))
a b c d e
0 0 0 0 0 0
1 1 1 1 1 1
2 2 2 2 2 2
3 3 3 3 3 3
4 4 4 4 4 4
Upvotes: 4
Reputation: 4322
You can set them individually if you're only dealing with a few columns:
df['b'] = df['a']
df['c'] = df['a']
or you can use a loop as you discovered.
Upvotes: 0
Reputation: 5660
Turns out you can use a loop to do this:
for i in ['b','c']: df[i] = df.a
Upvotes: 2