Skip to content Skip to sidebar Skip to footer

Add Multiple Columns To DataFrame And Set Them Equal To An Existing Column

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[

Solution 1:

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.


Solution 2:

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

Solution 3:

Turns out you can use a loop to do this:

for i in ['b','c']: df[i] = df.a

Solution 4:

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.


Post a Comment for "Add Multiple Columns To DataFrame And Set Them Equal To An Existing Column"