3

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?

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

1

You can do it column by column:

import pandas as pd
df = pd.DataFrame(index=range(5))
cols = ['A', 'B', 'C', 'D', 'E']
vals = [1, 2, 3, 4, 5]
for c, v in zip(cols, vals):
    df[c] = v
print df

Note that the last method mentioned in the other question you referred works similarly by creating each column before hand:

for a in attrlist:
    df[a] = 0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.