Reputation: 45
I have a series that is a list of lists that contain integers that I am attempting to turn into an array. This is a small snip-it of the list I am trying to convert into an array.
['[0, 0, 0, 0, 0, 0, 0, 0, 0, 1]',
'[0, 0, 0, 0, 0, 0, 0, 0, 1, 0]',
'[0, 0, 0, 0, 0, 0, 0, 1, 0, 0]',
'[0, 0, 0, 0, 0, 0, 0, 1, 0, 1]',
'[0, 0, 0, 0, 0, 0, 0, 1, 1, 1]']
I've tried to replace the quotes with .replace, but that hasn't worked out.
sequence = [i.replace(" '' ", ' ') for i in sequence]
Upvotes: 0
Views: 51
Reputation: 50949
You can use ast.literal_eval
to change the string to list of lists of ints
sequence = [literal_eval(i) for i in sequence]
# [[0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1, 0, 1], [0, 0, 0, 0, 0, 0, 0, 1, 1, 1]]
You can change it to numpy
array
import numpy as np
array = np.asarray(sequence)
print(array)
output
[[0 0 0 0 0 0 0 0 0 1]
[0 0 0 0 0 0 0 0 1 0]
[0 0 0 0 0 0 0 1 0 0]
[0 0 0 0 0 0 0 1 0 1]
[0 0 0 0 0 0 0 1 1 1]]
Or to 1d pandas
array
import pandas as pd
array = pd.array([item for items in sequence for item in items])
print(array)
outout
<IntegerArray>
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1]
Length: 50, dtype: Int64
Upvotes: 2