I am trying to import a simple tab separated text file using genfromtxt. I need to have access to each column header name, along with the data in the column associated with that name. Currently I am accomplishing this in a way that seems kind odd. All values in the txt file, including the header, are decimal numbers.
sample input file:
1 2 3 4 # header row
1.2 5.3 2.8 9.5
3.1 4.5 1.1 6.7
1.2 5.3 2.8 9.5
3.1 4.5 1.1 6.7
1.2 5.3 2.8 9.5
3.1 4.5 1.1 6.7
table_data = np.genfromtxt(file_path) #import file as numpy array
header_values = table_data[0,:] # grab first row
table_values = np.delete(table_data,0,0) # grab everything else
I know there must be a more proper way to import a text file of data. I need to make it easy to access each column’s header and the respective data pertaining to that header value. I appreciate any help you can provide.
Clarification:
I want to be able to access a column of data by using something along the lines of table_values[header_of_first_column]. How would I accomplish this?
Use the names parameter to use the first valid line as column names:
For example, if I modify the data you posted to be truly tab-separated, then the following code works: