1

Numpy array as single element of pandas dataframe

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!

Submitted May 20th 2021 by Admin

Answers
0

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

Admin | 3 months ago



Relevant Questions