What these commands do:
data_file = file_folder + file_name
tells Python what the name of the file is and where to find it! Tip: If you have trouble determining how to specify the file_folder location for your data, an easy workaround is to first put the data in the same folder as your Python program, then either (a) set file_folder = ''
as in the example above or (b) replace np.loadtxt(file_folder + file_name, ...)
with np.loadtxt(file_name, ...)
.
delimiter = ','
tells
loadtxt
that your data is in comma separated variable format (CSV). The character used to separate one column of data from another is called the 'delimiter.' See the
numpy.loadtxt manual page for details.
skiprows = 1
tells loadtxt
to skip over one row of text in the CSV file before looking for data to load into arrays. This is because the first row of text contains names for each column of data instead of data values (as shown in Table \ref{296817}).
unpack = True
tells loadtxt
that the data is in a 'packed' data format (in which each variable corresponds to a different column instead of to a different row )and therefore needs to be 'unpacked' when loaded. This is the typical arrangement for data in spreadsheets. Use unpack = False
if the data is in an 'unpacked' data format (in which each variable corresponds to a different row instead of a different column ).
usecols = (0, 1, 2)
says the data you are looking for is in the first 3 columns, which are numbered 0, 1, and 2 (because Python always starts from zero when counting). In our case, since there are only 3 columns of data and we want to use all three, this command is unnecessary. You could leave it out and everything would work just fine for this particular data file. If, however, you wanted to load data from a file with many columns but only needed data from column number 0, column number 3, and column number 4, you would need to include usecols = (0,3,4)
within the loadtxt
command.
angle, V_pd, V_pd_error = np.loadtxt(...)
tells Numerical Python to create an array called angle
and fill it with values from the first column of data in the CSV spreadsheet file, then create an array called V_pd
and fill it with values from the second column of data, and finally create an array called V_pd_error
and fill it with values from the third column of data. The result is three shiny new numpy data arrays we can use in our calculations.
What if your data is in a different text file format (such as tab delimited)? In that case you can still use
loadtxt
to import your data as long as you modify the
delimiter
command to match the file format. See the
numpy.loadtxt manual page for details.
saving data to a CSV file
For completeness, we now provide an example of how to use the savetxt
command from numpy
to save data in csv format. There are many ways to save files in spreadsheet format but this is the simplest method we've found so far using numpy
.
Direct numpy import style: