Access range of elements from an array Python

2024/10/12 14:23:37

Considering the following dataset:

>>> data[:10]
array([('T',  2,  8,  3, 5, 1,  8, 13, 0, 6,  6, 10,  8, 0,  8, 0,  8),('I',  5, 12,  3, 7, 2, 10,  5, 5, 4, 13,  3,  9, 2,  8, 4, 10),('D',  4, 11,  6, 8, 6, 10,  6, 2, 6, 10,  3,  7, 3,  7, 3,  9),('N',  7, 11,  6, 6, 3,  5,  9, 4, 6,  4,  4, 10, 6, 10, 2,  8),('G',  2,  1,  3, 1, 1,  8,  6, 6, 6,  6,  5,  9, 1,  7, 5, 10),('S',  4, 11,  5, 8, 3,  8,  8, 6, 9,  5,  6,  6, 0,  8, 9,  7),('B',  4,  2,  5, 4, 4,  8,  7, 6, 6,  7,  6,  6, 2,  8, 7, 10),('A',  1,  1,  3, 2, 1,  8,  2, 2, 2,  8,  2,  8, 1,  6, 2,  7),('J',  2,  2,  4, 4, 2, 10,  6, 2, 6, 12,  4,  8, 1,  6, 1,  7),('M', 11, 15, 13, 9, 7, 13,  2, 6, 2, 12,  1,  9, 8,  1, 1,  8)], dtype=[('f0', 'S1'), ('f1', '<i8'), ('f2', '<i8'), ('f3', '<i8'), ('f4', '<i8'), ('f5', '<i8'), ('f6', '<i8'), ('f7', '<i8'), ('f8', '<i8'), ('f9', '<i8'), ('f10', '<i8'), ('f11', '<i8'), ('f12', '<i8'), ('f13', '<i8'), ('f14', '<i8'), ('f15', '<i8'), ('f16', '<i8')])

How one can access all elements except the first one from each array. I am using the following code but to no avail.

>>> nd.array([[x[1:] for x in data[:10]])

Any help is really appreciated.

Answer

You can access all column names with the dtype.names attribute and then slice it:

>>> data[list(data.dtype.names[1:])]
array([( 2,  8,  3, 5, 1,  8, 13, 0, 6,  6, 10,  8, 0,  8, 0,  8),( 5, 12,  3, 7, 2, 10,  5, 5, 4, 13,  3,  9, 2,  8, 4, 10),( 4, 11,  6, 8, 6, 10,  6, 2, 6, 10,  3,  7, 3,  7, 3,  9),( 7, 11,  6, 6, 3,  5,  9, 4, 6,  4,  4, 10, 6, 10, 2,  8),( 2,  1,  3, 1, 1,  8,  6, 6, 6,  6,  5,  9, 1,  7, 5, 10),( 4, 11,  5, 8, 3,  8,  8, 6, 9,  5,  6,  6, 0,  8, 9,  7),( 4,  2,  5, 4, 4,  8,  7, 6, 6,  7,  6,  6, 2,  8, 7, 10),( 1,  1,  3, 2, 1,  8,  2, 2, 2,  8,  2,  8, 1,  6, 2,  7),( 2,  2,  4, 4, 2, 10,  6, 2, 6, 12,  4,  8, 1,  6, 1,  7),(11, 15, 13, 9, 7, 13,  2, 6, 2, 12,  1,  9, 8,  1, 1,  8)],dtype=[('f1', '<i8'), ('f2', '<i8'), ('f3', '<i8'), ('f4', '<i8'), ('f5', '<i8'), ('f6', '<i8'), ('f7', '<i8'), ('f8', '<i8'),('f9', '<i8'), ('f10', '<i8'), ('f11', '<i8'), ('f12', '<i8'), ('f13', '<i8'), ('f14', '<i8'), ('f15', '<i8'), ('f16', '<i8')])
https://en.xdnf.cn/q/118191.html

Related Q&A

Python - Remove extended ascii

Okay, so I am new to the whole python world so bear with me. Background: We are trying to offload logs into mongo to be able to query and search for them quicker. The device already prints them in a de…

Selenium - Python - Select dropdown meun option - No ID or Name

I am trying to select and element in a dropdown menu:The HTML is:<div class="col-lg-6"><select data-bind="options: indicator_type_list,value: indicatorType,optionsCaption: Choos…

How to prevent triples from getting mixed up while uploading to Dydra programmatically?

I am trying to upload some data to Dydra from a Sesame triplestore I have on my computer. While the download from Sesame works fine, the triples get mixed up (the s-p-o relationships change as the obje…

Adding a new row to a dataframe in pandas for every iteration

Adding a new row to a dataframe with correct mapping in pandasSomething similar to the above question.carrier_plan_identifier ... hios_issuer_identifier 1 AU…

Twisted client protocol - attaching an interface frontend

I am following the tutorial on writing a client/server pair in Twisted located here:http://twistedmatrix.com/documents/current/core/howto/clients.htmlI have everything working for the communication of …

Get query string as function parameters on flask

Is there a way to get query string as function parameters on flask? For example, the request will be like this.http://localhost:5000/user?age=15&gender=MaleAnd hope the code similar to this.@app.…

cython.parallel cannot see the difference in speed

I tried to use cython.parallel prange. I can only see two cores 50% being used. How can I make use of all the cores. i.e. send the loops to the cores simultaneously sharing the arrays, volume and mc_vo…

Is it possible (how) to add a spot color to pdf from matplotlib?

I am creating a chart which has to use (multiple) spot colors. This color could be one that is neither accessible from RGB nor CMYK. Is there a possibility to specify a spot color for a line in matplot…

Separate keywords and @ mentions from dataset

I have a huge set of data which has several columns and about 10k rows in more than 100 csv files, for now I am concerned about only one column with message format and from them I want to extract two p…

Kivy class in .py and .kv interaction 2

Follow up from Kivy class in .py and .kv interaction , but more complex. Here is the full code of what Im writing: The data/screens/learnkanji_want.kv has how I want the code to be, but I dont fully un…