Various ways to extract columns in a NumPy array

Various methods for retrieving columns in a two-dimensional array.

import

python


import numpy as np

Array generation

First, generate a 5x4 array appropriately

python


a = np.arange(5*4).reshape([5, 4])
print(a)
# [[ 0  1  2  3]
#  [ 4  5  6  7]
#  [ 8  9 10 11]
#  [12 13 14 15]
#  [16 17 18 19]]

Extract a specific column

python


#2nd row only
b = a[:, 2]
print(b)
# [ 2 6 10 14 18]

If ʻa [row, column]is specified, the row is specified in therow part and the column is specified in the columnpart. Since we want to retrieve columns this time, therow part is always: . By writing only: , it means that no particular row is specified. In other words, ʻa [:, 2] Means, "From all rows, only elements with an index of 2".

Extract multiple consecutive columns

Extract multiple consecutive columns, such as the 1st, 2nd, and 0th to 2nd columns.

python


# 1,2nd row
b = a[:, 1:3]
print(b)
# [[ 1  2]
#  [ 5  6]
#  [ 9 10]
#  [13 14]
#  [17 18]]

Since 1: 3 means 1 or more and less than 3, the 1st and 2nd columns can be extracted. If you specify like 1: or : 3, you can get all columns above and below the first column.

python


#1st row~
b = a[:, 1:]
print(b)
# [[ 1  2  3]
#  [ 5  6  7]
#  [ 9 10 11]
#  [13 14 15]
#  [17 18 19]]

# 0~2nd row
b = a[:, :3]
print(b)
# [[ 0  1  2]
#  [ 4  5  6]
#  [ 8  9 10]
#  [12 13 14]
#  [16 17 18]]

Even columns, odd columns, etc.

python


#Generate a 4x8 array
a = np.arange(4*8).reshape([4, 8])
# [[ 0  1  2  3  4  5  6  7]
#  [ 8  9 10 11 12 13 14 15]
#  [16 17 18 19 20 21 22 23]
#  [24 25 26 27 28 29 30 31]]

#Even columns
b = a[:, ::2]
print(b)
# [[ 0  2  4  6]
#  [ 8 10 12 14]
#  [16 18 20 22]
#  [24 26 28 30]]

#Odd column
b = a[:, 1::2]
print(b)
# [[ 1  3  5  7]
#  [ 9 11 13 15]
#  [17 19 21 23]
#  [25 27 29 31]]

In this way, since it basically follows the operation method of the list, it is possible to obtain it in multiples or in reverse order.

python


#Columns that are multiples of 3 from the back
b = a[:, ::-3]
print(b)
# [[ 7  4  1]
#  [15 12  9]
#  [23 20 17]
#  [31 28 25]]

#Skip one row from the first row to less than the seventh row
b = a[:, 1:7:2]
print(b)
# [[ 1  3  5]
#  [ 9 11 13]
#  [17 19 21]
#  [25 27 29]]

Extract multiple columns irregularly

Personally, the main subject is from here. There are times when you want to retrieve columns irregularly, such as columns 1, 5, and 6. It takes a lot of work, but it can be achieved by preparing a filter with the same length as the number of columns.

python


#Index 1, 5,The value of 6 is True,Other than that, a list of False lengths 8
f = [False, True, False, False, False, True, True, False]

b = a[:, f]
print(b)
# [[ 1  5  6]
#  [ 9 13 14]
#  [17 21 22]
#  [25 29 30]]

#Generate a boolean 1x8 zero matrix,A pattern that changes only the index value of the desired column to True
f = np.zeros(8, dtype=bool)
columns = [1, 5, 6]
f[columns] = True

b = a[:, f]
print(b)
# [[ 1  5  6]
#  [ 9 13 14]
#  [17 21 22]
#  [25 29 30]]

#If you want to get other than the specified column, generate an array of all True with ones,Change only unnecessary columns to False
f = np.ones(8, dtype=bool)
columns = [1, 5, 6]
f[columns] = False

b = a[:, f]
print(b)
# [[ 0  2  3  4  7]
#  [ 8 10 11 12 15]
#  [16 18 19 20 23]
#  [24 26 27 28 31]]

I think that most of the arrays handled by ~~ numpy have a large number of columns, so I think the second method will be the main one. The example uses np.zeros, but in some situations it may be easier to create an array of np.ones with all the elements True and then change only the columns you don't need to False. . ~~

python


#Generate a 4x8 array
a = np.arange(4*8).reshape([4, 8])
# [[ 0  1  2  3  4  5  6  7]
#  [ 8  9 10 11 12 13 14 15]
#  [16 17 18 19 20 21 22 23]
#  [24 25 26 27 28 29 30 31]]

# 1, 5,Take out 6 rows
b = a[:, [1, 5, 6]]
print(b)
# [[ 1  5  6]
#  [ 9 13 14]
#  [17 21 22]
#  [25 29 30]]

in conclusion

I thought that it would be difficult to understand even if the story was expanded too much, so this time I focused on columns, but it is also possible to extract a specific column of a specific row by specifying a row as well as a column. In the case of 3D or more By applying these methods, you can extract the desired elements.

The method of creating a zero matrix and extracting the columns, which I explained at the very end, seems to have been pretty clean for me, but I think there may be other easier methods ~~.

Postscript

I added it because @nkay told me a smarter way to retrieve multiple columns in the comments. (July 4, 2020)

Recommended Posts

Various ways to extract columns in a NumPy array
Various ways to create a dictionary (memories)
Various ways to create an array of numbers from 1 to 10 in Python.
How to sort by specifying a column in the Python Numpy array.
[Python] List Comprehension Various ways to create a list
Various ways to read the last line of a csv file in Python
Set the number of elements in a NumPy one-dimensional array to a power of 2 (0 padded)
Convert NumPy array "ndarray" to lilt in Python [tolist ()]
Try to solve Sudoku in various ways (SAT, CSP)
2 ways to read all csv files in a folder
Create a python numpy array
How to slice a block multiple array from a multiple array in Python
Various ways to calculate the similarity between data in python
How to extract other than a specific index with Numpy
Extract array elements and indexes in descending order with numpy
I want to align the significant figures in the Numpy array
How to get a quadratic array of squares in a spiral!
Subscript access to python numpy array
How to make a string into an array or an array into a string in Python
numpy: I want to convert a single type ndarray to a structured array
6 ways to string objects in Python
I wrote a script to extract a web page link in Python
5 Ways to Create a Python Chatbot
Extract multiple elements with Numpy array
Invert numpy Boolean array in tilde
Create an empty array in Numpy to add rows for each loop
How to use calculated columns in CASTable
I want to print in a comprehension
How to get a stacktrace in python
How to extract polygon area in Python
[V11 ~] A memorandum to put in Misskey
Various comments to write in the program
Various ways to destroy resources with scope
Adding Series to columns in python pandas
Function to extract the maximum and minimum values ​​in a slice with Go
A confusing story with two ways to implement XGBoost in Python + overall notes
Save an array of numpy to a wav file using the wave module
Extract elements (using a list of indexes) in a NumPy style from a Python list / tuple
Various ways to execute .py files on Windows
3 ways to parse time strings in python [Note]
Try to calculate a statistical problem in Python
[Python] Road to a snake charmer (4) Tweak Numpy
How to clear tuples in a list (Python)
To execute a Python enumerate function in JavaScript
How to embed a variable in a python string
Add rows to an empty array with numpy
I want to create a window in Python
How to install pip, numpy in Autodesk MAYA
[Python] Swapping rows and columns in Numpy data
How to create a JSON file in Python
A clever way to time processing in Python
How to implement a gradient picker in Houdini
Steps to develop a web application in Python
How to extract coefficients from a fractional formula
Convert a multidimensional list (array) to one dimension
How to check / extract files in RPM package
To add a module to python put in Julialang
Extract lines containing a specific "string" in Pandas
How to notify a Discord channel in Python
Output in the form of a python array
[Python] How to draw a histogram in Matplotlib