0

now :

inputs.values[:2]
Out[12]: array(['[24,28]', '[8,15,9]'], dtype=object)

i want to get numpy 2D array like this

[[1,2,3],[2,3,4]]

how to transform ?

evan
  • 15
  • 3
  • `inputs.tolist()` may be more useful. An object array does do anything for you. Simply `eval` each string. – hpaulj Aug 07 '20 at 05:27

1 Answers1

1

You can use literal_eval() and tolist() as below:

import pandas as pd 
import ast
import numpy as np

inputs = pd.Series(['[11, 21, 8]', '[18, 65, 108]'])
converted_inputs=np.array([ast.literal_eval(row) for row in inputs.tolist()])
print("converted_inputs=\n",converted_inputs)

The output is:

converted_inputs=
 [[ 11  21   8]
 [ 18  65 108]]
Roohollah Etemadi
  • 1,243
  • 1
  • 6
  • 18