0

I have a numpy array and an empty dataframe:

element = numpy.array([1,2,3])
df = pandas.DataFrame(columns = ["Col"])

I want to insert element in the first row of df. The following code:

df["Col"] = element

Gives me a dataframe 3x1 whose elements are 1, 2 and 3. I want a dataframe 1x1 whose element is the array. How can I get this result? Thanks in advance!

2 Answers 2

3

Use DataFrame.loc or DataFrame.at for specify label for set array to DataFrame:

df.loc[0, "Col"] = element
print (df)
         Col
0  [1, 2, 3]

df.at[0, "Col"] = element
Sign up to request clarification or add additional context in comments.

Comments

2

Wrap element in a list.

>>> df['Col'] = [element]
>>> df
         Col
0  [1, 2, 3]

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.