简体   繁体   中英

python, numpy, select rows, columns

There is numpy array, 1000 rows, 1000cols: a=np.array([[1, 2,3,4,5,6,7,8,9,...,1000],[1, 2,3,4,5,6,7,8,9,...,1000],[1, 2,3,4,5,6,7,8,9,...,1000],...,[1, 2,3,4,5,6,7,8,9,...,1000]])

How to get ONE array by selecting rows and cols from a like in this description: select full rows of row index: 0:2, 4:7, 101, and 156:989 ; and select full columns of columns index: 0:3, 400:589, 678, 701:999.

Edited , expanded rows and cols to 1000 rows, 1000cols, and expanded rows and cols selection criteria.

Hi! Any person out there! Please help solve. Or, please tell numpy development people to make things easy? selecting multiple row col segments like the question here is common and reasonable task. Make an efficient and concise solution please.

(hard to believe. supposed so many thinking heads for numpy and this task is a void? hard to believe. which one is more 'helpful' for productivity, negativity or positivity?)

It would be helpful if you could provide a simpler example of what you pretend with a clear example of both input and desired result. As far as I've understood, your problem comes down to slice the array in such a way that it has isolated lines as well as ranges, the key for it. While I could not figure it out inside NumPy, Pandas handles it perfectly with df.iloc .

Note: if you were looking for to select full columns and full rows separately, unless your row slice is equal to the column slice, it would not work as the shapes differ (x rows,1000 columns) (1000 rows, y columns). If you only care about the values, I leave a solution below. df.append simply stacks the arrays on top of each other (the .T transposes the array so that both have 1000 columns)

Answering to your question:

import numpy as np
import pandas as pd

a = np.arange(1,1000*1000+1).reshape(1000,1000)

sliced_a= (pd.DataFrame(a).iloc[
[*range(0,2),*range(4,7),*range(156,989),101],
[*range(0,3),*range(400,589),*range(701,999),678]
]).values

# Note Solution
pd.DataFrame(a).iloc[
[*range(0,2),*range(4,7),*range(156,989),101],:].append(pd.DataFrame(a).iloc[
:,[*range(0,3),*range(400,589),*range(701,999),678]].T).values

While it's less neat than the slicing syntax would be, this one works. In essence we are unpacking the ranges thus creating long lists of integers that we want to select. df.iloc is exactly what you expect but only accepts either slicing or lists of integers.

Finally, there are appropriate means and ways to request features and extensions to the already great work made by the OpenSource Community in modules such as NumPy. Take a minute to be grateful for those who work and keep pushing the edge of what is possible. Happy Coding!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM