Question:
I think this issue is pretty basic, but I haven’t seen it answered online. I’m using python and I have ‘pandas’ installed to make things easier. If there’s a way to do it without ‘pandas’ that would be awesome too! I’m coding a node connected map. I want to be able to take in some .csv file with a ‘previous’ and ‘next’ node list. I want this data to be then read by the program and stored in a list. For example:.csv file:
Name | Previous | Next |
---|---|---|
Alpha | one two | Three |
Beta | four | five |
Charlie | six | seven eight |
what I want in my program:
alpha,one two,three
and another way I’ve heard to solve this is use ” marks and separate with a comma:
alpha,"one,two",three
Although I have heard about these answers before, I haven’t been able to implement them. When reading the data in my program, it will assume that the space is part of the string or that the comma is part of the string.
[one, two]
my program will have a list containing one string that looks like ["one,two"]
or [one two]
I can change the way the variables are structured in the .csv file or the code reading in the data. Thanks for all the help in advance!
Answer:
There are multiple ways of doing this. Each for a different way you start with CSV data.First method will have the data in CSV as a single row with lists of things:
apply
to change the values. The convert function will just split the string using ,
as the delimiter.Name
with the values in CSV:agg
function to aggregate. The convert
function drops the duplicates and returns as a list.If you have better answer, please add a comment about this, thank you!