Reputation: 978
I have a csv file with this structure:
id;matrix
1;[[1.2 1.3] [1.2 1.3] [1.2 1.3]]
I'm trying read the matrix field as numpy.ndarray
using pandas.read_csv
to read and making df.to_numpy()
to convert the matrix, but the shape
array result in (1,0). I was waiting for the shape
equals (3,2) as:
matrix = [[1.2 1.3]
[1.2 1.3]
[1.2 1.3]]
I was try too numpy.asmatrix
, but the result is like df.to_numpy()
Upvotes: 0
Views: 67
Reputation: 62523
pandas
matrix
column is consistent with that shown in the example, replace the spaces with ,
, then use literal_eval
to turn the string into a list of lists, and then apply np.array
.import pandas as pd
from ast import literal_eval
import numpy as np
# read the data
df = pd.read_csv('file.csv', sep=';')
# replace the spaces
df['matrix'] = df['matrix'].str.replace(' ', ',')
# apply literal_eval
df['matrix'] = df['matrix'].apply(literal_eval)
# apply numpy array
df['matrix'] = df['matrix'].apply(np.array)
print(type(df.iloc[0, 1]))
>>> numpy.ndarray
ndarray
df['matrix'] = df['matrix'].apply(lambda x: np.array(literal_eval(x)))
df['matrix'] = df['matrix'].str.replace(' ', ',').apply(lambda x: np.array(literal_eval(x)))
Upvotes: 1