1

I'm trying to append a 3x2 numpy array to an existing dataframe. Something like this:

import pandas as pd
import numpy as np
df = pd.Dataframe({"A": [0,0,0], "B": [1,1,1]})
arr = np.arange(6).reshape(3, 2)

df[["C", "D"]] = arr  # NOPE!

How do I get this to work?

2 Answers 2

5

Use concat while converting your array to a dataframe:

df = pd.concat([df, pd.DataFrame(arr, columns=["C", "D"])], axis=1)

   A  B  C  D
0  0  1  0  1
1  0  1  2  3
2  0  1  4  5
Sign up to request clarification or add additional context in comments.

Comments

0

It didn't work because you need to pass a df:

arr = pd.DataFrame(np.arange(6).reshape(3, 2))

df[["C", "D"]] = arr #YEP

#Output
    A   B   C   D

0   0   1   0   1

1   0   1   2   3

2   0   1   4   5

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.