Do you want to combine two matrices in python? If yes then you have come to the right place. In this tutorial, you will How to Combine Two Column Matrices in Python steps.

## What is the matrix?

Matrix is an array of numbers, symbols, or expressions, arranged in rows and columns. There are many applications of the matrix in many fields. For example, you can use it in linear algebra, and calculus, statistics which is a branch of mathematics. You can also use it in computer science, and physics.

In the next section, you will know all the steps to Combine Two Column Matrices in Python.

## Steps on How to Combine Two-Column Matrices in Python

### Step 1: Import the required libraries

The first step is to import all the necessary libraries that you will use in this tutorial. I use the numpy and pandas libraries so let’s import them using the import statement.

```
import numpy as np
import pandas as pd
```

### Step 2: Create a sample matrix

The next step is to create a dummy matrix for implementation. You can easily create a dummy matrix using the np.array() function.

Execute the below lines of code to create the sample matrix.

```
matrix1 = np.array([[10], [20], [30]])
matrix2 = np.array([[40], [50], [60]])
```

### Step 3: Combine Two Column Matrices in Python using the below methods.

Now in this step, you will know all the methods that you can use to Combine Two Column Matrices in Python.

#### Method 1: Using np.hstack() function

The np.hstack() function allows you to combine the two matrixes horizontally. The resulting matrix will contain the same number of rows as the original matrix but the number of columns will be twice.

Execute the below lines of code to combine the matrix.

```
import numpy as np
import pandas as pd
matrix1 = np.array([[10], [20], [30]])
matrix2 = np.array([[40], [50], [60]])
combined_matrix = np.hstack((matrix1, matrix2))
print(combined_matrix)
```

**Output**

#### Method 2: Using the numpy concanate() function

The numpy package also provides the conacanate() function that accepts the two matrices as an argument to combine them. But you have to also pass the axis value as 1 to combine the elements column-wise.

Run the below lines of code to combine them.

```
import numpy as np
import pandas as pd
matrix1 = np.array([[10], [20], [30]])
matrix2 = np.array([[40], [50], [60]])
combined_matrix = np.concatenate((matrix1, matrix2), axis=1)
print(combined_matrix)
```

**Output**

#### Method 3: Combine using the pandas concat() function

You can also use the pandas concat() function to combine the two matrices. Here you will pass the two matrices with the axis =1 as an argument.

You will be able to combine the matrices when you will run the below lines of code.

```
import numpy as np
import pandas as pd
matrix1 = pd.DataFrame(np.array([[10], [20], [30]]))
matrix2 = pd.DataFrame(np.array([[40], [50], [60]]))
combined_matrix = pd.concat([matrix1, matrix2], axis=1)
print(combined_matrix)
```

**Output**

#### Method 4: Using built-in Python functions

In this method, you will use the list comprehension with the zip function to combine the two matrices.

```
import numpy as np
import pandas as pd
matrix1 = [[10], [20], [30]]
matrix2 = [[40], [50], [60]]
combined_matrix = [row1 + row2 for row1, row2 in zip(matrix1, matrix2)]
print(combined_matrix)
```

**Output**

## Conclusion

In this tutorial you have learned the numpy hstack() function, concatenate, and pandas concat() function to combine two matrices. You can use any of them according to your convenience. In each method, you will get the new matrices without changing the original matrices.

I hope you have liked this tutorial. If you have any queries then you can contact us for more help and information.

#### Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.