[Python] Importing csv files and creating numpy arrays

In summary, the code to load the csv file into an array and sum the counts looks incorrect. You need to read and discard the values you don't want from the first 21 rows, and then iterate through the array to find the sum of the counts.
  • #1
dcrisci
45
0
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 :

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:
Technology news on Phys.org
  • #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.
 
  • #3
dcrisci said:
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 :

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?
Most likely in the same directory as your python file is in, unless you provide the full path and filename to a different directory.
dcrisci said:
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?
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.
dcrisci said:
3. Does the coding I have to load the csv file look correct?
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.
dcrisci said:
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.
 

FAQ: [Python] Importing csv files and creating numpy arrays

How do I import a csv file into Python?

To import a csv file into Python, you can use the csv module or the pandas library. The csv module allows you to read and write csv files, while pandas provides more functionality for working with dataframes. You can use the read_csv() function from the pandas library to directly load a csv file into a dataframe.

How do I create a numpy array from a csv file?

To create a numpy array from a csv file, you can use the numpy library and the genfromtxt() function. This function allows you to specify the delimiter, data type, and other parameters for loading the csv file into a numpy array.

How do I handle missing or invalid data in a csv file?

To handle missing or invalid data in a csv file, you can use the filling_values parameter in the genfromtxt() function. This allows you to specify a value to replace any missing or invalid data in the csv file. Alternatively, you can use the fillna() function from the pandas library to fill in missing data in a dataframe.

Can I import a csv file from a URL?

Yes, you can import a csv file from a URL using the urllib or requests library. These libraries allow you to make HTTP requests and retrieve data from a URL. You can then use the retrieved data to create a dataframe or numpy array using the methods mentioned above.

How do I save a numpy array to a csv file?

To save a numpy array to a csv file, you can use the tofile() or savetxt() function from the numpy library. These functions allow you to specify the file name, delimiter, and other parameters for saving the numpy array to a csv file. You can also use the to_csv() function from the pandas library to save a dataframe to a csv file.

Back
Top