-1

I have a function that takes 2 inputs. I want it to read a csv file and return a one dimensional list that contains data from every row in a successive manner. I want my function to list every vote counts for every nominee in a list.

Code of my function is:

import csv
import pandas as pd

def retreiveData(filename,nominees):
    file=open(filename)
    csv_reader=csv.reader(file)
    result=[]

    return result


nominees=["Obama","Romney","Johnson","Stein","Others"]
retreiveData("ElectionUSA2012.csv",nominees)

There is some data in "ElectionUSA2012.csv".It contains vote counts for each nominee.

Vickel
  • 7,879
  • 6
  • 35
  • 56
ibra
  • 1
  • 1
  • Your code opens the file, reads the contents, creates an empty list and returns the empty list. What next steps are needed, do you think? How will you check each line? What do you need to check for each line? How do you add the relevant information from the line to the result? When calling the function, how do you capture its result? (also, do you want to use `csv` or `pandas` - they can both do the job, with some work of your own, but you don't need both) – Grismar Dec 15 '22 at 21:35
  • 1
    Does this answer your question? [Python import csv to list](https://stackoverflow.com/questions/24662571/python-import-csv-to-list) – Em Ae Dec 15 '22 at 21:38
  • Could you edit your question to include a small sample of whats inside `ElectionUSA2012.csv`? – Undesirable Dec 15 '22 at 21:42

2 Answers2

0

why dont you use pd.read_csv() ? And convert dataframe to list:

    df = pd.read_csv(filename)
    result = df.to_numpy().tolist() #if you want list or numpy array 
0

How about this one?

import pandas as pd

def retrieve_data(csv_file_path, nominees):
    df = pd.read_csv(csv_file_path)
    # https://stackoverflow.com/questions/11346283/renaming-column-names-in-pandas
    counts = df.sum(axis = 0, skipna = True).values.tolist()
    # https://www.geeksforgeeks.org/python-pandas-dataframe-sum/
    counts = {k:v for k, v in zip(nominees, counts)}
    
    return counts

csv_file_path = 'csv.csv'
counts = retrieve_data(csv_file_path, nominees=["Obama", "Romney", "Johnson", "Stein", "Others"])
print(counts)

for a csv file like this:

A   B   C   D    E
1   2   3   4    6
4   4   2   3    6
3   3   3   10   6

It would print a dictionary like this:

{'Obama': 8, 'Romney': 9, 'Johnson': 8, 'Stein': 17, "Others": 18}
Masoud Masoumi Moghadam
  • 1,094
  • 3
  • 23
  • 45