-3

I stored 2D numpy arrays into a pandas dataframe, which I stored into a csv file. While reading the csv file, I am struggling to get back my 2D numpy array as they are stored as type string... How could I get the numpy array that is inside the string?

For example I have '[1.34 5.43]' and I want to convert to [1.34 5.43]. Any way to convert numpy array interpreted as string into numpy array?

SOLUTION: This was the solution How to convert string representation of list to a list?

user7924113
  • 169
  • 1
  • 3
  • 15
  • https://stackoverflow.com/questions/3518778/how-do-i-read-csv-data-into-a-record-array-in-numpy/26296194 – Michael Teguh Laksana Jul 01 '20 at 13:20
  • Does this answer your question? [numpy reading a csv file to an numpy array](https://stackoverflow.com/questions/52252496/numpy-reading-a-csv-file-to-an-numpy-array) – Mad Physicist Jul 01 '20 at 13:23
  • Does this answer your question? [How do I read CSV data into a record array in NumPy?](https://stackoverflow.com/questions/3518778/how-do-i-read-csv-data-into-a-record-array-in-numpy) – wwii Jul 01 '20 at 13:25
  • How large were those arrays? It is awkward, though not impossiible to recover arrays that were saved a strings to a csv. It's better to use a different save method. – hpaulj Jul 01 '20 at 15:05

1 Answers1

0

You can use the genfromtxt function.

import numpy as np
#create the array 
arr=np.arange(20).reshape(4,5)
#save as a text file
np.savetxt("arr.csv", arr, delimiter=",")
from numpy import genfromtxt
#generate array from text file
my_data = genfromtxt('arr.csv', delimiter=',')
type(my_data)

Output

numpy.ndarray
Ricky
  • 2,662
  • 5
  • 25
  • 57
  • That's not exactly what I want. The numpy arrays are stored as string in the csv file. When I load the csv file, I want that each cell to be interpreted as a numpy array and not as a string – user7924113 Jul 01 '20 at 13:30
  • How does your csv file looks like ? Here the csv file is in string but genfromtxt converts into a numpy array type. – Ricky Jul 01 '20 at 13:31
  • I don't want to store all the values as a numpy array. I want to load my values in a pandas dataframe with each value being a numpy array – user7924113 Jul 01 '20 at 13:41
  • @user7924113 I think without any reproducible example of input and output it will be a bit hard to understand your problem – Ricky Jul 02 '20 at 02:33
  • This was the solution https://stackoverflow.com/questions/1894269/how-to-convert-string-representation-of-list-to-a-list – user7924113 Jul 02 '20 at 07:22