Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating columns dynamically. Assigning them a constant row vector

Tags:

python

pandas

Say I have some dataframe df. I would like to add to it four columns ['A', 'B', 'C, 'D'] that do not exist yet, and that will hold a constant row vector [1, 2, 3, 4].

When I try to do:

df[new_columns] = [1,2,3,4]

it fails (saying ['A', 'B', 'C, 'D'] is not in index).

How can I create multiple columns dynamically in Pandas? Do I always have to use append for something like this? I remember reading (e.g. in @Jeff's comment to this question) that in newer versions the dynamic creation of columns was supported. Am I wrong?

like image 861
Amelio Vazquez-Reina Avatar asked Nov 26 '25 03:11

Amelio Vazquez-Reina


1 Answers

I think this is the way to go. Pretty clear logic here.

In [19]: pd.concat([df,DataFrame([[1,2,3,4]],columns=list('ABCD'),index=df.index)],axis=1)
Out[19]: 
  label  somedata  A  B  C  D
0     b  1.462108  1  2  3  4
1     c -2.060141  1  2  3  4
2     e -0.322417  1  2  3  4
3     f -0.384054  1  2  3  4
4     c  1.133769  1  2  3  4
5     e -1.099891  1  2  3  4
6     d -0.172428  1  2  3  4
7     e -0.877858  1  2  3  4
8     c  0.042214  1  2  3  4
9     e  0.582815  1  2  3  4

Multi-assignment could work, but I don't think its a great soln because its so error prone (e.g. say some of your columns already exists, what should you do?). And the rhs is very problematic as you normally want to align, so its not obvious that you need to broadcast.

like image 188
Jeff Avatar answered Nov 28 '25 01:11

Jeff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!