Skip to content
Advertisement

How to set a numpy array in a pandas data frame cell?

I have a pandas dataframe. I want to fill some of the cells with numpy array but I get the following ValueError.

I wil not fill with zero array in real life. This is the simplified example code to replicate the error

ValueError: could not broadcast input array from shape (10,) into shape (1,)

JavaScript

Advertisement

Answer

One workaround solution is to use .map() with filtering of cell with .loc as you did, as follows:

This works since .map() works on transforming element-wise and would not try to broadcast array to the whole series.

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement