2
votes

I have a dataframe with a column full of numpy arrays.

    A     B         C
0   1.0   0.000000  [[0. 1.],[0. 1.]]
1   2.0   0.000000  [[85. 1.],[52. 0.]]
2   3.0   0.000000  [[5. 1.],[0. 0.]]
3   1.0   3.333333  [[0. 1.],[41. 0.]]
4   2.0   3.333333  [[85. 1.],[0. 21.]]

Problem is, when I save it as a CSV file, and when i load it on another python file, the numpy column is read as text.

I tried to transform the column with np.fromstring() or np.loadtxt() but it doesn't work.

Example of and array after pd.read_csv()

"[[ 85.  1.]\n [   52.            0.        ]]"

Thanks

Did you consider saving it in another format than csv, such as feather, parquet, or HDF? - Adrien Pacifico
Yes I did, and it does work. But I wanted to know if there is another way, admitting that I want it to be humanly readable when saved as CSV. - Mrofsnart
In short, you cannot, but you could provide a short function to perform the conversion - mozway