In this Python tutorial, we will learn** how to use a 3-dimensional NumPy array in Python**. Also, we will cover these topics.

- Python numpy 3d array slicing
- Python numpy 3d array to 2d
- Python numpy 3d array axis
- Python plot 3d numpy array
- Python 3d list to numpy array
- Python numpy transpose 3d array
- Python numpy sum 3d array
- Python numpy define 3d array
- Python numpy rotate 3d array
- Python numpy 3d example
- Python numpy where 3d array
- Python numpy empty 3d array
- Reshape 3d array to 2d python numpy
- Python numpy initialize 3d array
- Python numpy append 3d array
- Python numpy concatenate 3d array

**Table of Contents**show

## Python Numpy 3d array

- In this section, we will discuss how to create a 3-dimensional array in Python.
- Numpy provides a function that allows us manipulate data that can be accessed. A three dimensional means we can use nested levels of array for each dimension.
- To create a 3-dimensional numpy array we can use simple
**numpy.array()**function to display the 3-d array.

**Example:**

Let’s take an example and understand how to create a three-dimensional array with a specific value.

**Source Code:**

```
import numpy as np
arr1 = np.array([[[2,17], [45, 78]], [[88, 92], [60, 76]],[[76,33],[20,18]]])
print("Create 3-d array:",arr1)
```

Here is the implementation of the following given code

Also, read, Python NumPy Minimum

### How to create 3d numpy array in Python

By using the NumPy** reshape()**, we can easily create 3d NumPy array in Python. In Python, this method is used to shape a NumPy array without modifying the elements of the array.

**Example:**

```
import numpy as np
new_arr = np.array([[ 78, 23, 41, 66],
[ 109, 167, 41, 28],
[ 187, 22, 76, 88]])
b = new_arr.reshape(3, 2, 2)
print(b)
```

In the above code first, we have imported the Python NumPy library and then, create an array by using the **np.array**. Now use the **reshape()** method, in which we have passed the array shape and size.

Here is the Screenshot of the following given code

Read: Python NumPy Array + Examples

## Python numpy 3d array slicing

- In this Program, we are going to discuss how to create a numpy 3d array by using slicing in Python.
- To slice a array in Python we can easily use the indexing and this method we take an elements from one index to another index.
- In Python the slicing steps are
**start:end:step**. The first parameter is start if we do not pass this parameter in example then by default it takes as**0**. While in the case of end parameter it will be consider as the length of the array.

**Example:**

Let’s take an example and slice elements in a **Python NumPy array**.

```
import numpy as np
new_arr2 = np.array([[[178, 189, 567], [145, 239, 445], [197, 345, 678]],
[[56, 78, 190], [46, 10, 11], [6, 2, 1]],
[[45, 118, 203], [72, 119, 34], [87, 9, 5]]])
d= new_arr2[:2, 1:, :2]
print("slicing array:",d)
```

In the above code, we have just created a simple array and then apply the slicing method to it. In this example, we have selected the length of the array as **2**.

Here is the output of the following given code

Read: Check if NumPy Array is Empty in Python

## Python Numpy 3d array to 2d

- In this section, we will discuss how to convert a 3-dimensional numpy array to a two-dimensional array in Python.
- To perform this particular task we can use the numpy reshape() method and this function will help the user to reshape three-dimensional array to
**2-d**array. In Python reshape means we can easily modify the shape of the array without changing the elements.

**Syntax:**

Here is the Syntax of numpy.reshape() method

```
numpy.reshape
(
arr,
newshape,
order='C'
)
```

**Source Code:**

```
import numpy as np
new_arr2 = np.array([[[13, 9],
[161, 23]],
[[128, 219],
[109, 992]],
[[42, 34],
[ 128, 398]],
[[236, 557],
[645, 212]]])
b= np.reshape(new_arr2,(4,4))
print(b)
```

In the above program, we have passed array **‘new_arr’** along with the size of an array (no. of rows and no. of columns). Once you will print **‘b’ **then the output will display the new array.

Here is the Screenshot of the following given code

Read: Python NumPy Sum + Examples

## Python numpy 3d array axis

- In this Program, we will discuss how to create a 3-dimensional array along with an axis in Python.
- Here first, we will create two numpy arrays
**‘arr1’**and**‘arr2’**by using the**numpy.array()**function. Now use the concatenate function and store them into the**‘result’**variable. In Python, the concatenate method will help the user to join two or more numpy arrays of the same shape along with the axis. - In this example, we set the axis as
**0**which represents arrays that have been joined horizontally.

**Source code:**

```
import numpy as np
arr1 = np.array([[2,6,7],[16,14,111]])
arr2 = np.array([[73,27,41],[77,21,19]])
result = np.concatenate([arr1, arr2], axis = 0)
print(result)
```

Here is the output of the following given code

Read: Python NumPy zeros + Examples

## Python plot 3d numpy array

- Here we can see
**how to plot a 3-dimension numpy array in Python**. - In this example we have imported the matplotlib library for plotting the
**3-d**graph along with that we have imported the mpl_toolkits module for axes 3d and it is used for adding new axes to it of type axes 3d. - Here we can define the ‘result’ as a typical subplot with a 3-dimensional projection and then use the slicing method for creating the line object.
- Once you will use
**plt.figure()**then it creates a figure object and**plt.show()**opens one interactive window that displays our figure.

**Source Code:**

```
import matplotlib.pyplot as plt, numpy as np
from mpl_toolkits.mplot3d import Axes3D
arr1= np.array([[52,89,54], [103,423,934], [897,534,118]])
new_val = plt.figure()
result = new_val.add_subplot(122, projection='3d')
result.plot(arr1[:,0],arr1[:,1],arr1[:,2])
plt.show()
```

You can refer to the below Screenshot

Screenshot of the snippet:

Read: Python NumPy arange

## Python 3d list to numpy array

- Let us see how to convert the list into a
**3-d**numpy array by using Python. - In this example, we have to convert the list into a 3-dimension array. To do this task we are going to create a list named
**‘new_lis’**and then use the**np.asarray()**method for converting an input list to a numpy array and this function is available in the numpy module.

**Syntax:**

Here is the Syntax of **numpy.asarray()** method

```
numpy.asarray
(
a,
dtype=None,
order=None,
like=None
)
```

**Source** **Code:**

```
import numpy as np
new_lis = [[23, 45, 278],[ 189, 234, 445],[ 567, 421, 109],[ 18, 21, 188]]
new_output = np.asarray(new_lis)
print(new_output)
```

Here is the implementation of the following given code

As you can see in the Screenshot the output is a 3-dimension NumPy array in Python.

Read: Python NumPy append + 9 Examples

## Python numpy transpose 3d array

- In this section, we will discuss
**how to transpose a 3-dimension array in Python**. - Here in this example, we have created a simple numpy array in which passes an integer’s value. Now declare a variable
**‘result’**and use**np.transpose()**method. In Python, the**np.transpose()**method will help the user for changing the row items into column items and similar the column elements into row elements. - This method can transpose the
**3-d**array and the output of this method is an updated array of the given one.

**Syntax:**

Here is the Syntax of numpy.transpose() method

```
numpy.transpose
(
a,
axes=None
)
```

**Example:**

Let’s take an example and understand how to transpose a 3-dimensional array in Python

```
import numpy as np
new_arr = np.array([[23,45,21,78,91],[24,19,41,22,191],[39,84,12,34,44]])
result = np.transpose(new_arr)
print(result)
```

Here is the execution of the following given code

Read: Python sort NumPy array

## Python numpy sum 3d array

- In this program, we will discuss
**how to sum a 3-dimensional numpy array in Python**. - By using the
**np.sum()**method we can solve this problem. In Python, the**sum()**method sums up the items of an array and within the array object.

**Syntax:**

Here is the Syntax of np.sum() function

```
numpy.sum
(
arr,
axis=None,
dtype=None,
out=None,
keepdims=<no value>
initial=<no value>
where=<no value>
)
```

**Source Code:**

```
import numpy as np
arr1 = np.array([[[ 56, 24, 16],[ 17, 18, 29],
[64, 16, 18]],
[[ 24, 27, 36],[ 18, 19, 26],
[ 27, 13, 64]]])
b = np.sum(arr1,axis = 0)
print(b)
```

Here is the Screenshot of the following given code

Read: Python NumPy matrix

## Python numpy define 3d array

- In this section, we will discuss
**how to define a numpy 3-dimensional array by using Python**. - To define a 3-d array we can use
**numpy.ones()**method. In Python the**numpy.ones()**function fills values with one and it will always return a new numpy array of given shape.

**Syntax:**

Here is the Syntax of numpy.ones() method

```
numpy.ones
(
shape,
dtype=None,
order='C'
like=None
)
```

**Source Code:**

```
import numpy as np
arr1 = np.ones((3, 3, 3))
print(arr1)
```

In the above code first, we have to import a NumPy library and then create a variable **‘arr1’** in which we pass **np.ones()** method for defining a new 3-dimensional array.

Here is the Screenshot of the following given code

Read: Python NumPy linspace + Examples

## Python numpy rotate 3d array

- Let us see
**how to rotate a 3-dimensional numpy array in Python**. - By using the
**np.rot90**we can easily rotate the numpy array in 90 degrees. In Python, this method is used to rotate a NumPy array by 90 degrees.

**Syntax:**

Here is the syntax NumPy.rot90() method

```
numpy.rot
(
m,
k=1,
axes=(0,1)
)
```

**Source Code:**

```
import numpy as np
arr1 = np.array([[16,18], [24,43], [17,19]])
print(arr1)
b = np.rot90(arr1, 3)
print("After rotating arr:",b)
```

You can refer to the below Screenshot

As you can see in the Screenshot the output is the rotation of the array.

Read: Python NumPy concatenate + 9 Examples

## Python numpy where 3d array

- Let us see
**how to use where function in a 3-dimensional array by using Python**. - In Python, this method is used for selecting items based on a condition and it always returns items chosen from
**X**and**Y**and this function is available in the Python Numpy module.

**Syntax:**

Here is the Syntax of numpy.where() method

```
numpy.where
(
condition
[,
x,
y
]
)
```

**Example:**

```
import numpy as np
arr1 = np.array([[56, 1, 12], [3, 2, 14],[3,21,5]])
result = np.where(arr1<6)
print(result)
print(arr1[result])
```

In the above code, we have created an array and then use np. where() method in which we assign the condition **a<6**. Once you will print ‘result’ then the output will display a new 3-dimension array.

Here is the Screenshot of the following given code

Read: Python NumPy log + Examples

## Python numpy empty 3d array

- Here we can see
**how to create an empty 3-dimension array by using Python**. - In this example, we are going to use an
**np.empty()**method for creating an empty array. In Python, this function does not set the values to zero. It takes only random values.

**Syntax:**

Here is the Syntax of np.empty() function in Python

```
numpy.empty
(
shape,
dtype=float,
order='C'
)
```

**Note:** These parameters define the shape, datatype, and order. It will always return the array of uninitialized data.

**Source Code:**

```
import numpy as np
arr1 = np.empty((3, 3, 3))
print(arr1)
```

Here is the execution of the following given code

Read: Python NumPy read CSV

## Reshape 3d array to 2d python numpy

- In this Program, we will discuss
**how to reshape 3-dimensional array to 2-dimensional numpy array in Python**. - In Python reshape means we can easily modify the shape of the array without changing the elements.

**Syntax:**

Here is the Syntax of NumPy.reshape() method.

```
numpy.reshape
(
arr,
newshape,
order='C'
)
```

**Source Code:**

```
import numpy as np
new_arr2 = np.array([[[42, 16],
[567, 123]],
[[345, 136],
[445, 890]],
[[567, 123],
[789, 908]],
[[678, 908],
[645, 212]]])
result= np.reshape(new_arr2,(4,4))
print(result)
```

Once you will print **‘result’** then the output will display the array of 4*4 dimensions.

Here is the execution of the following given code

Read: Python NumPy to list

## Python numpy initialize 3d array

- In this section, we will discuss
**how to initialize a 3-dimensional array in Python**. - In Python to initialize a 3-dimension array, we can easily use the np.array function for creating an array and once you will print the
**‘arr1’**then the output will display a 3-dimensional array.

**Source Code:**

```
import numpy as np
arr1 = np.array([[[4,36], [134, 94]], [[976, 234], [189, 123]],[[56,21],[109,67]]])
print("Initialize 3-d array:",arr1)
```

Read: Python NumPy square with examples

## Python numpy append 3d array

- In this section, we will discuss
**how to append numpy 3d array by using Python**. - In Python, the
**append()**function will add items at the end of an array and this function will merge two numpy arrays and it always returns a new array.

**Example:**

Let’s take an example and understand how to append a 3-dimensional numpy array in Python

```
import numpy as np
new_array1 = np.array([[23, 31], [78, 89],[356,921]])
new_array2 = np.array([[834, 567], [179, 119],[823,108]])
result = np.append(new_array1, new_array2,axis=1)
print(result)
```

In the above code, we apply the append() function in which we have assigned two given arrays **‘new_array1’** and **‘new_array2’**. Once you will print the ‘result’ then the output will display a new updated 3-dimensional array.

Here is the Screenshot of the following given code

Read: Python NumPy absolute value with examples

## Python numpy concatenate 3d array

- Let us see
**how to concatenate a 3-dimensional numpy array by using Python**. - In Python, the concatenate function is used to combine two different numpy arrays along with an axis.
- In this example we have created a two numpy arrays
**‘arr1’**and**‘arr2’**by using**np.array()**function. Now use concatenate function in which we have pass arrays and axis it.

**Source Code:**

```
import numpy as np
arr1 = np.array([[67, 23, 89], [15, 35, 76],[114,256,345]])
arr2 = np.array([[48, 189, 234],[782, 567, 190],[543,134,567]])
print(np.concatenate((arr1, arr2), axis = -1))
```

Here is the output of the following given code

As you can see in the screenshot the output will display a new 3-d array.

You may also like to read the following Python Numpy tutorials.

- Python NumPy Average with Examples
- Python NumPy empty array with examples
- Python NumPy shape with examples
- Python NumPy 2d array + Examples
- Python NumPy diff

In this Python tutorial, we have learned** how to use a 3-dimensional NumPy array in Python**. Also, we have covered these topics.

- Python numpy 3d array slicing
- Python numpy 3d array to 2d
- Python numpy 3d array axis
- Python plot 3d numpy array
- Python 3d list to numpy array
- Python numpy transpose 3d array
- Python numpy sum 3d array
- Python numpy define 3d array
- Python numpy rotate 3d array
- Python numpy 3d example
- Python numpy where 3d array
- Python numpy empty 3d array
- reshape 3d array to 2d python numpy
- Python numpy initialize 3d array
- Python numpy append 3d array
- Python numpy concatenate 3d array

Entrepreneur, Founder, Author, Blogger, Trainer, and more. Check out my profile.