0

I'm trying to convert what seems to be a string into an array of arrays. I'm uploading an excel sheet using read_excel but one of the columns is formatted:

[[u'MAKER', u'CREATED', u'1463538547', None], ['SHOP', u'ACCEPTED', u'1463538651', None], [u'SHOP', u'READY', u'1463539221', None], [u'COURIER', u'COMPLETED', u'1463540801', None]]

It's reading this as a complete string but I want it to be read as an array of arrays so I loop over each array. This is what I'm using to intake the file

changes = pd.read_excel('dataset.xlsx', sheet_name = "changes").

I tried changing the type to list but that doesn't seem to help.

Any help is greatly appreciated.

Thank You!

1
  • What is the column name that has the array of arrays Commented Nov 5, 2019 at 5:30

2 Answers 2

2

You can use ast.literal_eval

>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
Sign up to request clarification or add additional context in comments.

Comments

0

Can you try this? change 'array_column_name' to the actual column name

changes = pd.read_excel('dataset.xlsx', sheet_name = "changes",converters={'array_column_name':ast.literal_eval})

2 Comments

I get an error of TypeError: unhashable type: 'list'
I was able to figure it out. Every time i tried to do a second loop to view the inner array it would treat the array as a string so it would loop through the letters. Doing the below got me what i wanted, thanks! for row in changes['array_list']: for item in ast.literal_eval(row): print(item)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.