0

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 ?

anxiousPI
  • 183
  • 1
  • 12
  • Can you explain what you desired end result is? It's a little unclear what you're trying to do. – wgwz Oct 07 '21 at 13:23
  • I want to store a numpy.array in cells of a Pandas.DataFrame using df.at, i updated the post so it would be clearer. – anxiousPI Oct 07 '21 at 13:24

2 Answers2

1

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.

9769953
  • 10,344
  • 3
  • 26
  • 37
  • am i able to assign to the dataframe without setting the column as an object? i.e. by setting dtype of the array as an int ? – anxiousPI Oct 07 '21 at 13:34
  • No: one dtype per column. Otherwise Pandas (and the underlying NumPy) loses its logic and speed. – 9769953 Oct 07 '21 at 13:35
  • In fact, if you want to do that (that is, what you already are doing now), you should consider a different datastructure, or generally a different approach (perhaps the array is better off in a column of its own). – 9769953 Oct 07 '21 at 13:36
  • I agree with you that changing the approach is better, but unfortunately I cannot do that. Thanks for the help ! – anxiousPI Oct 07 '21 at 13:39
-1

change int to object import numpy as np import pandas as pd

arr = np.array([[1, 2], [3, 4]], dtype=object)

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

  • You have now *initialized* the dataframe with the same array that is used in the later assignment. This is *not* what the OP shows: they initialize their dataframe with a 2D list of ints. – 9769953 Oct 07 '21 at 17:09