How to add a new column in a matrix with numpy ?

Active March 19, 2019    /    Viewed 6128    /    Comments 0    /    Edit


Examples of how add a new column in a matrix with numpy:

Let's consider an array of size (10,4):

\begin{equation}
A = \left( \begin{array}{ccc}
4 & 11 & 10 & 2 \\
8 & 18 & 5 & 10 \\
3 & 15 & 17 & 13 \\
14 & 15 & 17 & 16 \\
4 & 11 & 2 & 10 \\
1 & 14 & 5 & 11 \\
16 & 5 & 12 & 8 \\
17 & 17 & 15 & 0 \\
4 & 5 & 12 & 8 \\
6 & 2 & 19 & 15
\end{array}\right)
\end{equation}

>>> import numpy as np
>>> a = np.array([[ 4, 11, 10,  2],
...        [ 8, 18,  5, 10],
...        [ 3, 15, 17, 13],
...        [14, 15, 17, 16],
...        [ 4, 11,  2, 10],
...        [ 1, 14,  5, 11],
...        [16,  5, 12,  8],
...        [17, 17, 15,  0],
...        [ 4,  5, 12,  8],
...        [ 6,  2, 19, 15]])

Add a column in first position

We want for example to add a new column in the first position containing the line indexes:

>>> table_shape = table.shape
>>> table_shape
(10, 4)
>>> b = np.array([i for i in range(table_shape[0])])
>>> b
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

a solution is to use the numpy function c_, illustration:

>>> new_table = np.c_[b,a]
>>> new_table
array([[ 0,  4, 11, 10,  2],
       [ 1,  8, 18,  5, 10],
       [ 2,  3, 15, 17, 13],
       [ 3, 14, 15, 17, 16],
       [ 4,  4, 11,  2, 10],
       [ 5,  1, 14,  5, 11],
       [ 6, 16,  5, 12,  8],
       [ 7, 17, 17, 15,  0],
       [ 8,  4,  5, 12,  8],
       [ 9,  6,  2, 19, 15]])

\begin{equation}
A = \left( \begin{array}{ccc}
0 & 4 & 11 & 10 & 2\\
1 & 8 & 18 & 5 & 10\\
2 & 3 & 15 & 17 & 13\\
3 & 14 & 15 & 17 & 16\\
4 & 4 & 11 & 2 & 10\\
5 & 1 & 14 & 5 & 11\\
6 & 16 & 5 & 12 & 8\\
7 & 17 & 17 & 15 & 0\\
8 & 4 & 5 & 12 & 8\\
9 & 6 & 2 & 19 & 15
\end{array}\right)
\end{equation}

Note: another solution is to use the numpy function insert, example

>>> np.insert(a,0,b,axis=1)
array([[ 0,  4, 11, 10,  2],
       [ 1,  8, 18,  5, 10],
       [ 2,  3, 15, 17, 13],
       [ 3, 14, 15, 17, 16],
       [ 4,  4, 11,  2, 10],
       [ 5,  1, 14,  5, 11],
       [ 6, 16,  5, 12,  8],
       [ 7, 17, 17, 15,  0],
       [ 8,  4,  5, 12,  8],
       [ 9,  6,  2, 19, 15]])

Add a column in last position

>>> b = np.array(([99,99,99,99,99,99,99,99,99,99]))
>>> new_table = np.c_[a,b]
>>> new_table
array([[ 4, 11, 10,  2, 99],
       [ 8, 18,  5, 10, 99],
       [ 3, 15, 17, 13, 99],
       [14, 15, 17, 16, 99],
       [ 4, 11,  2, 10, 99],
       [ 1, 14,  5, 11, 99],
       [16,  5, 12,  8, 99],
       [17, 17, 15,  0, 99],
       [ 4,  5, 12,  8, 99],
       [ 6,  2, 19, 15, 99]])

\begin{equation}
A = \left( \begin{array}{ccc}
4 & 11 & 10 & 2 & 99\\
8 & 18 & 5 & 10 & 99\\
3 & 15 & 17 & 13 & 99\\
14 & 15 & 17 & 16 & 99\\
4 & 11 & 2 & 10 & 99\\
1 & 14 & 5 & 11 & 99\\
16 & 5 & 12 & 8 & 99\\
17 & 17 & 15 & 0 & 99\\
4 & 5 & 12 & 8 & 99\\
6 & 2 & 19 & 15 & 99)
\end{array}\right)
\end{equation}

Another solution, using insert, example

>>> np.insert(a,table_shape[1],b,axis=1)
array([[ 4, 11, 10,  2, 99],
       [ 8, 18,  5, 10, 99],
       [ 3, 15, 17, 13, 99],
       [14, 15, 17, 16, 99],
       [ 4, 11,  2, 10, 99],
       [ 1, 14,  5, 11, 99],
       [16,  5, 12,  8, 99],
       [17, 17, 15,  0, 99],
       [ 4,  5, 12,  8, 99],
       [ 6,  2, 19, 15, 99]])

Add a column for a given index

To add a column for a given index the best approach is to use insert:

Add a column at the index 2

>>> b = np.array(([99,99,99,99,99,99,99,99,99,99]))
>>> np.insert(a,2,b,axis=1)
array([[ 4, 11, 99, 10,  2],
       [ 8, 18, 99,  5, 10],
       [ 3, 15, 99, 17, 13],
       [14, 15, 99, 17, 16],
       [ 4, 11, 99,  2, 10],
       [ 1, 14, 99,  5, 11],
       [16,  5, 99, 12,  8],
       [17, 17, 99, 15,  0],
       [ 4,  5, 99, 12,  8],
       [ 6,  2, 99, 19, 15]])

Add a column at the index 3

>>> b = np.array(([99,99,99,99,99,99,99,99,99,99]))
>>> np.insert(a,3,b,axis=1)
array([[ 4, 11, 10, 99,  2],
       [ 8, 18,  5, 99, 10],
       [ 3, 15, 17, 99, 13],
       [14, 15, 17, 99, 16],
       [ 4, 11,  2, 99, 10],
       [ 1, 14,  5, 99, 11],
       [16,  5, 12, 99,  8],
       [17, 17, 15, 99,  0],
       [ 4,  5, 12, 99,  8],
       [ 6,  2, 19, 99, 15]])

etc.

References


Card image cap
profile-image
Daidalos

Hi, I am Ben.

I have developed this web site from scratch with Django to share with everyone my notes. If you have any ideas or suggestions to improve the site, let me know ! (you can contact me using the form in the welcome page). Thanks!



Did you find this content useful ?, If so, please consider donating a tip to the author(s). MoonBooks.org is visited by millions of people each year and it will help us to maintain our servers and create new contents.

Amount