Error while trying to store an array to dataframe cell

anxiousPI

I am trying to store an array to the data frame cell using df.at as below:

import numpy as np
import pandas as pd

arr = np.array([[123, 123], [123, 123]], dtype=int)

df = pd.DataFrame(data= [[1, 2], [3, 4]], columns=["A", "B"])
df.at[0, "A"] = arr

But I keep getting the following error:

ValueError: setting an array element with a sequence.

I tried solving this by following this thread, but unfortunately the solutions did not work for me.

I need to use df.at and not another method, Any help ?

9769953

It doesn't work, because you are trying to assign a NumPy array to a cell that has an integer dtype.

Try the following:

import numpy as np
import pandas as pd

a = np.array([[123, 123], [123, 123]], dtype=int)

df = pd.DataFrame(data=[[1, 2], [3, 4]], columns=["A", "B"]).astype({"A": object, "B": object})
df.at[0, "A"] = a

Result:

                          A  B
0  [[123, 123], [123, 123]]  2
1                         3  4

Be aware that your dtypes are now objects, not integers. If you only need to adjust cells in column "A", you can of course leave out "B" when changing the dtype above.

The underlying array at [0, "A"] is still of int(64) type.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Unexpected error while trying to store user with sequelize

error occurs while trying to update ngrx store

Rstudio: Error while trying to use %in% operator in a dataframe

Syntax error while trying to build dataframe in python

Getting error when trying to store loop to array

Trying to join 2 Dataframes, and store certain data as an array in one cell

AttributeError error while trying to split an array of numbers

Error while trying to iterate JSON array in JavaScript

#VALUE! Error while trying to use a referencing cell in a formula

Shape error when trying to color a cell of dataframe and then dump it as html and trying to retain style for cell

Getting error while trying to add ngrx store after ESLint

Getting error while trying to store Int, arrayof [string] pair in userDefaults

Signature algorithm error while trying to upload apk to Google Play Store

Reshape error while trying to store values in a vector in MATLAB

Error while trying to drop a row from pandas DataFrame index?

Error while trying to save the data to Hive tables from dataframe

Encoder error while trying to map dataframe row to updated row

Store a dataframe in one cell in postgres

Store setdiff results in dataframe cell

Error while trying to destructure an array of possible length 2

Error Trying to access array offset while activating WordPress debug

Validation error while trying to parse a json array to List[Object] in Scala

Getting error while trying to match two array using PHP

Getting error while trying to render array of object in react js

Getting Error : lvalue required while trying to implement a Jagged Array in C

Undefined error while trying to map over array of objects React Native

Why am I getting a "Permission denied error" while trying to read a cell value using openpyxl python?

Trying to store form input to array

Assertion failure while trying to expand UITableView cell