0

I am using numpy.genfromtxt and the usecols parameter allows filtering out columns (by non-inclusion). For a lengthy list of columns it is not ideal to explicitly mention every single one - given that only one column need not be included.

If the sequence were instead a list then we could use

  usecols=[1:]

However that is not possible for a sequence - which afaict needs to be like

 usecols=(1,2,3,4, .. and many many more ..)

Here is the specific invocation that is being attempted:

data = np.genfromtxt(path, delimiter=',',skip_header= 1,usecols=[1:])

That does not compile due to the invalid attempt to use the slicing syntax on the sequence. is there any alternative supported by sequences?

2
  • Possible duplicate of Pandas usecols all except last Commented May 8, 2019 at 2:20
  • maybe - but the answer given below was not included there and it's useful Commented May 8, 2019 at 2:26

1 Answer 1

1

Generate the complete column list with something like [i for i in range(n)] and then remove the desired element with remove() and pass this filtered list to the usecols.

Sign up to request clarification or add additional context in comments.

4 Comments

that's helpful: can you please add this specific code to your answer indats = np.genfromtxt(path, delimiter=',',skip_header= 1,usecols=(x for i,x in enumerate(range(18)) if i!=0))
No need for a list comprehension. range already does what you want: usecols=tuple(range(1,18))
Also, for future reference, there's no point using enumerate on a one-argument range - i and xwill always be the same.
@Tomothy32 I am basically unfamiliar with python sequences and their relation to .. well .. anything.. Just learnt they appear to be interchangeable with tuples

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.