I am using numpy.loadtxt to extract a large array of data from a text file and then using a loop to put different columns into different dictionary keys like this:
f = numpy.loadtxt(datafile, skiprows=5) # Open and read in the file, skipping to the data
d = {} # Create empty dictionary
for x in range(0, f.shape[1]):
d[x] = f[:,x] # Loop through the columns of the datafile, putting each one into
#a dictionary index
The row above the array in the text file contains all the titles for the variables in the array, is there a way to get each variable name and put it as the key name for the relevant dictionary? (ie, column one = data, d[date]={14/11/12,15/11/12 …. etc)
Pandas is a good idea, so “thumbs up” to the answer by reptilicus.
If you don’t want the dependency on Pandas, you can just as easily use the function
numpy.genfromtxtto read the data directly into a numpy structured array. A structure array acts like both a numpy 1-d array and a dictionary.For example, here’s a sample data file, “data.csv”:
You can read this into a structured array as follows:
The option
names=Truetellsgenfromtxtto use the columns headings as the field names in the structured array. Settingdtype=Nonetellsgenfromtxtto figure out the data type of the columns automatically (the default is to convert all values to double precision floating point values).datalooks like this.You can access individual elements (each is a structure containing three fields):
Or you can access columns using the dictionary-like interface:
And you can combine those: