Home > Software engineering >  How do you import a CSV whose elements are Numpy arrays?
How do you import a CSV whose elements are Numpy arrays?

Time:07-03

Due to using a combination of not entirely compatible software packages, I need to be able to read a CSV file whose elements are stylized as Numpy arrays. An example of such a CSV would be the one below:

enter image description here

When using genfromtxt('Input.csv',delimiter=',',dtype=None,encoding=None), I get output of the form:

[['[4 3 2 1]' '[1 1 0 0]']
 ['[1 3 4 2]' '[0 1 1 0]']]

What I would like is to get this into the form of a three-dimensional Numpy array like below:

[[[4 3 2 1] [1 1 0 0]]
 [[1 3 4 2] [0 1 1 0]]]

CodePudding user response:

You may create an array from these strings dynamically (at run-time):

    import numpy as np
    import ast

    # data represents the string returned by str(genfromtxt(...))
    data = '[[\'[4 3 2 1]\' \'[1 1 0 0]\'] [\'[1 3 4 2]\' \'[0 1 1 0]\']]'

    # remove quotes
    data = data.replace('\'', '')
    # insert commas
    data = data.replace(' ', ', ')

    # create an array 
    np_data = np.array(ast.literal_eval(data))

    # here's your 3D numpy array
    print(np_data.shape)
    print(np_data)
  • Related