6

So what I want to do is to add columns to a dataframe and fill them (all rows respectively) with a single value.

import pandas as pd
import numpy as np

df = pd.DataFrame(np.array([[1,2],[3,4]]), columns = ["A","B"])
arr = np.array([7,8])

# this is what I would like to do
df[["C","D"]] = arr

# and this is what I want to achieve
#    A  B  C  D
# 0  1  2  7  8
# 1  3  4  7  8
# but it yields an "KeyError" sadly
# KeyError: "['C' 'D'] not in index"

I do know about the assign-functionality and how I would tackle this issue if I only were to add one column at once. I just want to know whether there is a clean and simple way to do this with multiple new columns as I was not able to find one.

2 Answers 2

8

For me working:

df[["C","D"]] = pd.DataFrame([arr], index=df.index)

Or join:

df = df.join(pd.DataFrame([arr], columns=['C','D'], index=df.index))

Or assign:

df = df.assign(**pd.Series(arr, index=['C','D']))

print (df)
   A  B  C  D
0  1  2  7  8
1  3  4  7  8
Sign up to request clarification or add additional context in comments.

1 Comment

Nice answer. My take-away message is that there is no basic functionality to do is, but one has to choose one of your workarounds to achieve that. I would prefer the last of your three options.
1

You can using assign and pass a dict in it

df.assign(**dict(zip(['C','D'],[arr.tolist()]*2)))
Out[755]: 
   A  B  C  D
0  1  2  7  7
1  3  4  8  8

2 Comments

The "2" should probably not be hardcoded. I do also like this answer, jezaels answer just looks like to fit better into pandas programming style.
@Corrumpo if so using len(['C','D']) to replace the 2 :-)

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.