7

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
Gaurav Bansal
  • 5,221
  • 14
  • 45
  • 91

4 Answers4

6

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.

MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419
  • 2
    It's worth noting that eg. `df = df.assign(c='some_value', b='some_value')` does add the columns in alphabetical order to the original dataframe. So resulting dataframe will have columns a b c and not a c b. – stack_lech Apr 20 '17 at 10:20
  • 1
    @Cpt_Jauchefuerst, good point, thank you! I've added it to the answer – MaxU - stand with Ukraine Apr 20 '17 at 10:26
4

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
piRSquared
  • 285,575
  • 57
  • 475
  • 624
2

Turns out you can use a loop to do this:

for i in ['b','c']: df[i] = df.a
Gaurav Bansal
  • 5,221
  • 14
  • 45
  • 91
0

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.

Elliptica
  • 3,928
  • 3
  • 37
  • 68