Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8697195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:27:47+00:00 2026-06-13T01:27:47+00:00

I am using numpy.loadtxt to extract a large array of data from a text

  • 0

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)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:27:48+00:00Added an answer on June 13, 2026 at 1:27 am

    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.genfromtxt to 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”:

    alpha, beta, gamma
    100, 0.5, 19.9
    210, 0.25, 21.0
    240, 0.45, 15.0
    290, 0.75, 5.5
    

    You can read this into a structured array as follows:

    >>> data = genfromtxt('data.csv', delimiter=',', names=True, dtype=None)
    

    The option names=True tells genfromtxt to use the columns headings as the field names in the structured array. Setting dtype=None tells genfromtxt to figure out the data type of the columns automatically (the default is to convert all values to double precision floating point values).

    data looks like this.

    >>> data
    array([(100, 0.5, 19.9), (210, 0.25, 21.0), (240, 0.45, 15.0),
           (290, 0.75, 5.5)], 
          dtype=[('alpha', '<i4'), ('beta', '<f8'), ('gamma', '<f8')])
    

    You can access individual elements (each is a structure containing three fields):

    >>> data[0]
    (100, 0.5, 19.9)
    

    Or you can access columns using the dictionary-like interface:

    >>> data['beta']
    array([ 0.5 ,  0.25,  0.45,  0.75])
    

    And you can combine those:

    >>> data['beta'][1]
    0.25
    >>> data[1]['beta']
    0.25
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to read in data from a text file using numpy.loadtxt with
From a text file containing three columns of data I want to be able
I am loading a csv file via numpy.loadtxt into a numpy array. My data
I have an numpy array which I save to a image using savefig(). Then
When I load an array using numpy.loadtxt, it seems to take too much memory.
I am using Numpy to store data into matrices. Coming from R background, there
I am reading in a file using numpy.genfromtxt which brings in columns of both
I am using python with numpy to read in data from a numerical model
I'm using numpy to create a cube array with sides of length 100, thus
I am using Python's multiprocessing module to process large numpy arrays in parallel. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.