Skip to content
Advertisement

Converting a CSV column into a 1D list and then into a 2D list

Hope you are well,

I am fairly new in the python world and the coding world in general. I wanted to test python/panda’s powers and the usefulness of making lists.

I have a column called ‘Lines’ in my csv file which reads like this:

Lines
A,B,C
A,D,C
B,C,D,E
A,B,C,D,E
C
C,D,E

Which I converted to a 1D list (thanks to pandas):

JavaScript

Now I know there are a ton of tutorials out there but somehow none worked for me… The output I am searching for would be something like this:

JavaScript

Is this something that is doable in python I wonder? Creating a 2D list based on multiple items in a specific CSV row? Thanks for any help/resource provided! Apologies in advance if the template provided is not useful/up-to-standards.

R.

Advertisement

Answer

You can do something like:

JavaScript

lines is now a 2D array. You can access elements like you wanted to.

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement