0

I imported my CSV File and made the data into an array. Now I was wondering, what can I do so that I'm able to print a specific value in the array? For instance if I wanted the value in the 2nd row, 2nd column. Also how would I go about adding the two values together? Thanks.

import csv
import numpy as np
f = open("Test.csv")
csv_f = csv.reader(f)
for row in csv_f:
    print(np.array(row))
f.close()
7
  • You realise you aren't actually keeping any of that data, right? Why not use e.g. numpy.loadtxt? Commented Jul 23, 2015 at 16:05
  • Code doesn't show any idea. Better provide example values instead of just asking questions. Commented Jul 23, 2015 at 16:06
  • My code does actually show data from the imported CSV File so get your facts straight first... Commented Jul 23, 2015 at 16:11
  • 1
    It shows it, but doesn#'t store it; after each loop, the data is discarded. I believe @wenzul's point was that you appear to have made no effort to implement anything beyond that yourself. Commented Jul 23, 2015 at 16:15
  • I have managed to edit my code so that I can choose specific values from the array but I want to know what to do to add them Commented Jul 23, 2015 at 16:17

3 Answers 3

1

There is no need to use csv module.

This code reads csv file and prints value of cell in second row and second column. I am assuming that fields are separated by commas.

with open("Test.csv") as fo:
    table = [row.split(",") for row in fo.read().replace("\r", "").split("\n")]
print table[1][1]
Sign up to request clarification or add additional context in comments.

Comments

0

So, I grabbed a dataset ("Company Funding Records") from here. Then, I just rewrote a little...

#!/usr/bin/python

import csv
#import numpy as np

csvaslist = []

f = open("TechCrunchcontinentalUSA.csv")
csv_f = csv.reader(f)
for row in csv_f:
#    print(np.array(row))
    csvaslist.append(row)
f.close()

#  Now your data is in a dict.  Everything past this point is just playing
#  Add together a couple of arbitrary values...
print int(csvaslist[2][7]) + int(csvaslist[11][7])

#  Add using a conditional...
print "\nNow let's see what Facebook has received..."
fbsum = 0
for sublist in csvaslist:
    if sublist[0] == "facebook":
        print sublist
        fbsum += int(sublist[7])
print "Facebook has received", fbsum

I've commented lines at a couple points to show what's being used and what was unneeded. Notice at the end that referring to a particular datapoint is simply a matter of referencing what is, effectively, original_csv_file[line_number][field_on_that_line], and then recasting as int, float, whatever you need. This is because the csv file has been changed to a list of lists.

Comments

0

To get specific values within your array/file, and add together:

import csv

f = open("Test.csv")
csv_f = list(csv.reader(f))

#returns the value in the second row, second column of your file
print csv_f[1][1]

#returns sum of two specific values (in this example, value of second row, second column and value of first row, first column
sum = int(csv_f[1][1]) + int(csv_f[0][0])
print sum

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.