Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

[Python] Importing csv files and creating numpy arrays

  1. Apr 1, 2015 #1
    So I am fairly new to Python and feel somewhat comfortable with it. I am trying to do something what I think seems kind of simple however am not too experienced with loading files into Python (or any programming language for that matter).

    What I am trying to do is I have csv files that contain a bunch of Multi-channel analyzer properties in the first 21 rows and 7 columns (which I do not need to be loaded into the arrays I would like). Below these properties I have three columns for channel number, energy level, and the number of counts. I would like to load the column of counts into one numpy array and just sum them up to find the total number of counts. The coding I have this far is :

    Code (Python):
    """
    Created on Wed Apr 1 16:25:25 2015
    @author: dariocrisci
    """


    import numpy as np

    csv = np.genfromtxt('al_five_degrees.csv', delimiter= ",")

    Counts = csv[:,2]

    print(Counts)
    I understand this will only print the array of counts, I haven't done the summing up yet (which is just a simple numpy command) but I am getting an error for not finding the file. SOOOOO, my questions are:
    1. Where do the csv files need to be saved for python to find them?
    2. To only create an array of value of the number of counts, should I go into my csv files and remove the MCA properties and save them with only the three columns of values?
    3. Does the coding I have to load the csv file look correct?​
    I have read many threads saying to use pandas but I am not familiar with this module and am trying to remain with modules I am comfortable with.
     
    Last edited: Apr 1, 2015
  2. jcsd
  3. Apr 1, 2015 #2
    If you are not going to provide a full path but only a single file name, then, you need to run your python script from the same directory where your csv file is.

    Other than that, I don't feel like reading too much from your writing and speculate on how exactly your csv file look like, if you need more help, you need to post the file and maybe highlight the data you need. Having said that, you may need to read the documentation for genfromtxt and read about a couple other options like skipping lines so that you can get to where to need to be.
     
  4. Apr 1, 2015 #3

    Mark44

    Staff: Mentor

    Most likely in the same directory as your python file is in, unless you provide the full path and filename to a different directory.
    No, that's not a good idea. Just read and discard (i.e., don't store into variables) the values you don't want in the first 21 rows. For the following rows, read and discard the channel number and energy level, but store the counts number in your array.
    When you have read all of the counts values, iterate through your array to find the sum of the counts.
    I don't think so, but I'm not familiar with the numpy library. Typically when you're getting input from a file you have to open the file (using the open() function), and then you can read through the file, using read() (reads the whole file) or readline() (reads a single line - probably what you want).

    Here is some documentation about the genfromtxt() function, if you haven't already seen it.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: [Python] Importing csv files and creating numpy arrays
  1. Textread of csv-file (Replies: 9)

  2. C++ csv file reading (Replies: 2)

Loading...