0

I have a csv file with ";" separator where information are look like,

Id;arrVal
0;[[84, 109, 423, 466]]
1;[[242, 192, 822, 594], [242, 640, 818, 911]]
2;[[123, 212, 401, 351]]

I am reading the csv file and want to get the array for specific Id by using following code

df = pd.read_csv("path/to/csv/file.csv", sep=";")
df[df['Id']==1].iloc[0]['arrVal']

It gives me the value [[242, 192, 822, 594], [242, 640, 818, 911]] but as a string. How can I extract as a 2D array?

2 Answers 2

3

That column is also valid JSON syntax, so you could use:

import pandas as pd
import json

df = pd.read_csv('input.csv',sep=';')
L = json.loads(df[df['Id']==1].iloc[0]['arrVal'])
Sign up to request clarification or add additional context in comments.

Comments

2

Use ast.literal_eval:

x = ast.literal_eval(df[df['Id']==1].iloc[0]['arrVal'])

Comments

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.