Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
391 views
in Technique[技术] by (71.8m points)

python - How to read 1st column and 2nd column, and nth column to last column . Numpy

I have a multidimensional array and I need to select columns 1st and 2nd, 1st and 3rd, 1st and 4th respectively. Then the 2nd and 3rd,2nd and 4th and finally the 3rd and 4th columns. And etc... For the purposes of the statement, I transposed the array.

My code

import pandas as pd 

pole= np.array([[11,12,13,14],[21,22,23,24],[31,32,33,34],[41,42,43,44]])
pole=np.transpose(pole)
print(pole)

I need

#1st and 2nd
11 21
12 22
13 23
14 24
#1st and 3rd
11 31
12 32
13 33
14 34
#1st and 4th 
11 41
12 42
13 43
14 44
#2nd and 3rd
21 31
22 32
23 33
24 34
#2nd and 4th 
21 41
22 42
23 43
24 44
#3rd and 4th
31 41
32 42
33 43
34 44
question from:https://stackoverflow.com/questions/65924464/how-to-read-1st-column-and-2nd-column-and-nth-column-to-last-column-numpy

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could something like pole[:,0:2]. It will give the first two columns. For something like 2nd column and last column you could do pole[:,[1,3]]

The first param before the comma is used for describing the rows you want and the second for columns. : - means all of it 0:2 - means starting from 0th column till 2nd(not included) Or you can pass the list like [1,3] which means column at positions 1 and 3 (index starts with 0).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...