Home > Mobile >  Getting a numpy array string from a csv file and converting it to a numpy array
Getting a numpy array string from a csv file and converting it to a numpy array

Time:09-17

I made some calculations with my data and saved it into a csv file. In the file I have a cell with this string:

"[array([3, 3, 3]), array([3, 3, 3]), array([3, 3, 3]), array([3, 3, 3]), array([3])]"

I want to convert it to a valid numpy array. Tried some functions but got no luck yet.

CodePudding user response:

If you have pandas, use pd.eval:

>>> import pandas as pd
>>> from numpy import array
>>> pd.eval("[array([3, 3, 3]), array([3, 3, 3]), array([3, 3, 3]), array([3, 3, 3]), array([3])]")
[array([3, 3, 3]), array([3, 3, 3]), array([3, 3, 3]), array([3, 3, 3]), array([3])]
>>> 
  • Related