0

I was processing some data that I got while doing a project and coded this:

# Parse data from txt file
data = numpy.loadtxt(inFileName + '.txt', skiprows=3)
freq = data[:, 0]   # Mirror Frequency (Hz)
sdfreq = data[:, 1] # σ Frequency (Hz)
dist = data[:, 2]   # ∆X (m)
sddist = data[:, 3] # σ ∆X (m)

And I realized that the last 4 lines look repetitive and I was obviously not going to repeat doing that if I had 1000 more data parameters. I could obviously parse it into a dictionary but that would force me to call dataset['freq'][0] instead of simply freq[0]. I could also parse it into an object and call dataset.freq[0], which would be better. But, is there a way I can compact the code and still have the ability to use freq[0] (without using exec())?

4
  • If it were a tuple you could do freq, sdfreq, dist, sddist = data. I'm not sure what the numpy equivalent is. Commented Jan 14, 2021 at 20:46
  • Maybe freq, sdfreq, dist, sddist = data[:] Commented Jan 14, 2021 at 20:48
  • Why do you need to assign these columns to variables? I can see doing that for 4 columns, but 1000? There's nothing particularly wrong with those 4 lines. The unpacking suggested by others may be handy, but isn't necessarily clearer. Commented Jan 15, 2021 at 0:35
  • With dtype and names is possible to get a structured array, for which you could access fields by name, e.g. data['freq'] etc. Commented Jan 15, 2021 at 0:36

2 Answers 2

1

Just use unpack=True:

import numpy

data = numpy.loadtxt(inFileName + '.txt', skiprows=3 , unpack=True )
[freq, sdfreq, dist, sddist] = data

print(freq, sdfreq, dist, sddist)
Sign up to request clarification or add additional context in comments.

Comments

1

I'm not sure I understand every aspect of your problem but I think you are on the right track. Is this of some help to you?

import numpy as np

data = np.array([[1,2,3,4],[5,6,7,8]])

def assign(data):
    result = []

    for i in range(data.shape[1]):
        result.append(list(data[:,i]))

    return result

freq, sdfreq, dist, sddist = assign(data)

or shorter:

freq, sdfreq, dist, sddist = [list(data[:,i]) for i in range(data.shape[1])]

Explanation:

Instead of parsing the data into a dictionary you can assign the columns to individual variables in one step:

For each column ('i in range(data.shape1)') return the column 'data[:,i]' as you did manually.

Cave: you need to keep track of column indeces, if your dataset changes you need to insert a new variable at the exact position. Perhaps a structured table or pandas might be helpful in the long run (especially when it comes to 1000+ columns).

1 Comment

That is what I wanted, but Daniil's answer is more adequate for my problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.