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!

3
  • 1
    Possible duplicate of Python Read csv files in a column? Commented Jul 18, 2019 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) Commented Jul 18, 2019 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? Commented Jul 19, 2019 at 0:18

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

1 Comment

thanks! Can you please tell me how you the data in the above format ? @parthikb
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']

Comments

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]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.