How to convert string representation of list in pandas series to a numpy array?

evan

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 ?

Roohollah Etemadi

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]]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to convert a grouped pandas series to a numpy array

convert string representation of array to numpy array in python

Convert pandas series into numpy array

How to convert a pandas list row to numpy array?

How to convert string representation of list to a list?

How do I convert a pandas Series or index to a Numpy array?

How to convert pandas series with numpy array values to dataframe

How to convert the string representation of array to normal array

Easy way to convert string representation of numpy array with variable spacing and scientific notation into list

How to check if a variable is either a python list, numpy array or pandas series

How to convert a string representation to an array integers?

How to convert an array of Booleans to a string representation?

convert pandas series AND dataframe objects to a numpy array

Convert a pandas Series of lists into a numpy array

How can a string representation of a NumPy array be converted to a NumPy array?

How to test whether a pandas series contains elements from another list (or NumPy array or pandas series)?

Convert string representation of list into list

How to convert a numpy matrix to a pandas series?

How to convert vector wrapped as string to numpy array in pandas dataframe?

How to convert string representation list with mixed values to a list?

How I Convert a string representation of a nested list into a nested list?

How to convert List to numpy Array

How to convert a string to numpy array?

Convert List object in a pandas dataframe to numpy array

Convert a list in a pandas df column into a numpy array

How to convert list of values in a series into dataframe pandas

How to convert string representation of dictionary in Pandas DataFrame to a new columns?

How to convert numpy array saved as string in pandas csv file back to a numpy array?

convert multidimensional numpy array to list of string