0

I have a list for example fruitList = [Apple, Mango, Banana]. I have a csv file which contains a bunch of columns that includes the ones in the list. For example:

Orange Apple Strawberry Banana
   1    12      5         11
   2    3      10         9

How to I get a column of csv for example Banana as list?

What I tried so far:

data_file = open('fruit_data.csv')
data_reader = csv.reader(data_file, delimiter=",")
header = data_reader.next()

my_list = []

for field in header:
    for fruits in fruitList:
        if(fruits==field):
            mylist = header[fruits]

I know the last line of the code is wrong. I was wondering how to get the information in csv with the string variable as list index.

Thanks!

Rory Daulton
  • 21,934
  • 6
  • 42
  • 50
Mikasa
  • 321
  • 7
  • 16
  • 1
    Possible duplicate of [Python Read csv files in a column?](https://stackoverflow.com/questions/40735266/python-read-csv-files-in-a-column) – Dominic McLoughlin Jul 18 '19 at 23:33
  • @DominicMcLoughlin Looks similar. But its making a list of all columns. I want one single column at a time. For example. I want the banana column in the csv as [5,10](List) – Mikasa Jul 18 '19 at 23:41
  • there is no such thing as a string variable in Python. Python variables *do not have a type*. Objects have types. you cannot index into a list with a `str` object, you must use an `int` object. So, are you asking how to convert `str` objects to `int` objects? – juanpa.arrivillaga Jul 19 '19 at 00:18

3 Answers3

1

you can take the following steps:

import csv
list= [tuple(row) for row in csv.reader(open(filename, 'rU'))]

Above code will get you your data in the form :

list = [('Orange', 'Apple', 'Strawberry', 'Banana'), (1, 12, 5, 11), (2, 3, 10, 9)]

the next step you can perform is :

new_list = [x[3] for x in list]

output :

new_list = ['Banana', 11, 9]

I hope this solved your problem, if not, tell me. I'm happy to help!

Peace out //

Parthik Bhandari

Parthik B.
  • 472
  • 1
  • 5
  • 15
1

Ex.

from collections import defaultdict                                                                 
import csv                                                                                          

the_data = defaultdict(list)                                                                        

with open("fruit_data.csv", "r") as fd:                                                             
    reader = csv.DictReader(fd)                                                                     
    for row in reader:                                                                              
        for fruit, number in row.items():                                                           
            the_data[fruit].append(number)                                                          

for fruit, the_list in the_data.items():                                                            
    print fruit, the_list   

O/P:

Orange ['1', '2']

Strawberry [' 5', ' 10']

Apple [' 12', ' 3']

Banana [' 11', ' 9']
bharatk
  • 4,202
  • 5
  • 16
  • 30
Chris Curvey
  • 9,738
  • 10
  • 48
  • 70
0

My contribution using tolist() command from pandas:

import pandas as pd
data_file = pd.read_csv('fruit_data.csv')

# Creating data containers
fruitList = ['Apple', 'Mango', 'Banana']
names_columns = []
list_names = []

# Extracting the columns names from the imported cvs file 
columns = list(data_file)
for i in columns:
    names_columns.append(i)   

# Getting the columns of the csv file as lists when the criteria is meet
for fruit in fruitList:
    for name in names_columns:
        if name==fruit:
            list_names.append(name)

# Printing the desired lists            
    for name in list_names:
        print(name, data_file[name].tolist())

Apple [12, 3]
Banana [11, 9]
Ramon
  • 518
  • 6
  • 16