Convert numpy array to list in pandas

In this article we will discuss how to convert a 1D or 2D or 3D Numpy Array to a list or list of lists.

Table of Contents

  1. Convert a Numpy Array to a List
  2. Convert 2D Numpy array to list of lists
  3. Convert 2D Numpy array to a flat list
  4. Convert 3D Numpy array to nested lists
  5. Convert 3D Numpy array to a flat list

Convert Numpy Array to List

ndarray class of Numpy Module in Python provides a member function tolist[], which returns a list containing the copy of elements in array. So, we can use that to convert a numpy array to a list. For example,

import numpy as np # Create a Numpy Array arr = np.array[[11, 22, 33, 44, 55]] print['Numpy Array:', arr] # Convert Numpy Array to list num_list = arr.tolist[] print['List: ', num_list]

Output:

Numpy Array: [11 22 33 44 55] List: [11, 22, 33, 44, 55]

numpy.ndarray.tolist[] function returns a list [nested if required] containing copy of elements. But what if we try to convert a 2D Numpy array to list?

Advertisements

Convert 2D Numpy array to list of lists

When we call tolist[] function on a 2D numpy Array, then it returns a nested list i.e. a list of lists in python. Also, all elements in the list of lists will be the copy of elements in 2D numpy array. For example,

import numpy as np # Create 2D Numpy Array arr = np.array[[[1, 2, 3, 4], [5, 6, 7, 8], [3, 3, 3, 3]]] print['2D Numpy Array:'] print[arr] # Convert Numpy Array to list of lists list_of_lists = arr.tolist[] print['List of lists:'] print[list_of_lists]

Output:

2D Numpy Array: [[1 2 3 4] [5 6 7 8] [3 3 3 3]] List of lists: [[1, 2, 3, 4], [5, 6, 7, 8], [3, 3, 3, 3]]

Convert 2D Numpy Array to a flattened list

numpy.ndarray.tolist[] always returned a nested list for a 2D Numpy Array. But if we want to convert a 2D Numpy array to a flattened list i.e. a single list, then we need to first flattened the 2D Numpy array to 1D array and then call tolist[] function on it. For example,

import numpy as np # Create 2D Numpy Array arr = np.array[[[1, 2, 3, 4], [5, 6, 7, 8], [3, 3, 3, 3]]] print['2D Numpy Array:'] print[arr] # Convert 2D Numpy array toa single list num_list = arr.flatten[].tolist[] print['List:', num_list]

Output:

import numpy as np # Create 2D Numpy Array arr = np.array[[[1, 2, 3, 4], [5, 6, 7, 8], [3, 3, 3, 3]]] print['2D Numpy Array:'] print[arr] # Convert 2D Numpy array toa single list num_list = arr.flatten[].tolist[] print['List:', num_list]

Convert 3D Numpy array to nested list

Similar to previous examples, we can use the tolist[] function to convert a 3D Numpy array to list of nested lists. For example,

import numpy as np # Create 3D Numpy Array arr = np.ones[ [2,4,5] , dtype=np.int64] print['3D Numpy Array:'] print[arr] # Convert 3D Numpy Array to nested list nested_list = arr.tolist[] print['Nested list:'] print[nested_list]

Output:

3D Numpy Array: [[[1 1 1 1 1] [1 1 1 1 1] [1 1 1 1 1] [1 1 1 1 1]] [[1 1 1 1 1] [1 1 1 1 1] [1 1 1 1 1] [1 1 1 1 1]]] Nested list: [[[1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1]], [[1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1]]]

Convert 3D Numpy Array to a flat list

To convert a 3D numpy array to a single flat list, we need to first flatten the 3D numpy array to a 1D numpy array using the flatten[] function and then call tolist[] on that flatten array to create flat list. For example,

import numpy as np # Create 3D Numpy Array arr = np.ones[ [2,4,5] , dtype=np.int64] print['3D Numpy Array:'] print[arr] # Convert 3D Numpy Array to flat list flat_list = arr.flatten[].tolist[] print['Flat list:'] print[flat_list]

Output:

3D Numpy Array: [[[1 1 1 1 1] [1 1 1 1 1] [1 1 1 1 1] [1 1 1 1 1]] [[1 1 1 1 1] [1 1 1 1 1] [1 1 1 1 1] [1 1 1 1 1]]] Flat list: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

Summary:

We learned how to convert a 1D / 2D / 3D Numpy array to list or list of lists in python.

Advertisements

Video liên quan

Chủ Đề