• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: Python – how to store multiple variables in one column of a .csv file and then read those variables into a list

Resolved: Python – how to store multiple variables in one column of a .csv file and then read those variables into a list

0
By Isaac Tonny on 17/06/2022 Issue
Share
Facebook Twitter LinkedIn

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:
I have heard about two ways to write multiple variables in one .csv column. One way was placing a space in between the two values/variables: 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.
So, instead of having a list of two strings [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:
Note the quotation around the lists. We can use apply to change the values. The convert function will just split the string using , as the delimiter.
Second, each row is repeated for Name with the values in CSV:
We can you the agg function to aggregate. The convert function drops the duplicates and returns as a list.
The results should look like this:

If you have better answer, please add a comment about this, thank you!

csv list pandas python
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: How do I stop the command from happening if the requirements for it to work aren’t sert Discord.js

02/04/2023

Resolved: How to scroll bottom of div at launch?

02/04/2023

Resolved: how to get and read an xml file in a zip file using xml.etree

02/04/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.