Different ways to convert a Python dictionary to a NumPy array


NumPy is one of the popular libraries in python which is used to perform numerical calculations, scientific computations. It also allows us to work with large multi-dimensional arrays and matrices. There are many functions and modules in-built in the numpy library which are used to work with the different dimensional arrays.

Converting a dictionary into a NumPy array

We can convert a dictionary into a NumPy array by using the different modules and functions available in Numpy library. Let's see each way one by one.

Using the numpy.array() function

In NumPy, we have the function namely array(), which is used to create the array by passing the any data-structres such as dictionary, list etc. Following is the syntax for using the array() function of the NumPy library to convert a python dictionary to NumPy array.

import numpy 
numpy.array(dictionary_name)

Where,

  • numpy is the name of the library.

  • array is the function to create the array.

  • dictionary_name is the input argument as dictionary.

Example

In this example, we will create the NumPy array by using the array() function by passing the dictionary as the input argument then the dictionary will be converted into NumPy array.

import numpy as np
dic = {'Name': ['Karthikeya', 'Anil', 'Srivatsav', 'Riyaansh', 'Prasad'], 
       'Age': [1, 30, 25, 1, 55], 
       'Skill': ['python', 'Salesforce', 'Networking', 'python', 'Management']}
print("The dictionary to be converted into array:",dic)
arr = np.array(list(dic.values()))
print("The array created from the dictionary:",arr)
print(type(arr))

Output

The dictionary to be converted into array: {'Name': ['Karthikeya', 'Anil', 'Srivatsav', 'Riyaansh', 'Prasad'], 'Age': [1, 30, 25, 1, 55], 'Skill': ['python', 'Salesforce', 'Networking', 'python', 'Management']}
The array created from the dictionary: [['Karthikeya' 'Anil' 'Srivatsav' 'Riyaansh' 'Prasad']
 ['1' '30' '25' '1' '55']
 ['python' 'Salesforce' 'Networking' 'python' 'Management']]
<class 'numpy.ndarray'>

Using the numpy.asarray() function

We have another function namely asarray() in numpy library which has the similar functionality of the array() function. This function also takes the dictionary values as the input and returns the numpy array as output. Following is the syntax for using the asarray() function.

np.asarray(dictionary)

Example

In the following example, we will pass the dictionary as the input argument to the as array() function to convert the dictionary into numpy array.

import numpy as np
dic = {'Age': [1, 30, 25, 1, 55], 
       'year': [2002,2020,1995,1900,2023]}
print("The dictionary to be converted into array:",dic)
arr = np.asarray(list(dic.values()))
print("The array created from the dictionary:",arr)
print(type(arr))

Output

The dictionary to be converted into array: {'Age': [1, 30, 25, 1, 55], 'year': [2002, 2020, 1995, 1900, 2023]}
The array created from the dictionary: [[   1   30   25    1   55]
 [2002 2020 1995 1900 2023]]
<class 'numpy.ndarray'>

Using a loop

Using the loop, we can convert the dictionary into numpy array. Following is the example for converting the dictionary into numpy array.

import numpy as np
dic = {'Age': 20, 'year': 2023,'class':10}
arr = np.zeros(len(dic))
i = 0
for key in dic:
    arr[i] = dic[key]
    i += 1
print(arr)

Output

[  20. 2023.   10.]

Using numpy.fromiter() function

In Numpy, there is another function namely, fromiter() which is used to convert the dictionary into array. This function takes the dictionary values and datatype as the input arguments. The following is the syntax.

numpy.fromiter(dictionary,dtype = datatype)

Example

In this example, we will convert the dictionary into array by passing the dictionary as the input argument to the fromiter() function.

import numpy as np
dic = {'Age': 20, 'year': 2023,'class':10}
print("The dictionary to be converted into array:",dic)
arr = np.fromiter(dic.values(),dtype = int)
print("The array created from the dictionary:",arr)
print(type(arr))

Output

The dictionary to be converted into array: {'Age': 20, 'year': 2023, 'class': 10}
The array created from the dictionary: [  20 2023   10]
<class 'numpy.ndarray'>

Using the numpy.column_stack() function

When we want to convert the dictionary into multiple row array then we can go for column_stack() function of the numpy array. Following is the syntax.

numpy.column_stack(dictionary[key] for key in dictionary)

Example

Here we will pass the dictionary to the column_stack() function as input argument then the dictionary will be converted into array with multiple rows.

import numpy as np
dic = {'Age': [20,20], 'year': [2023,2022],'class':[10,9]}
print("The dictionary to be converted into array:",dic)
arr = np.column_stack(dic[key] for key in dic)
print("The array created from the dictionary:",arr)
print(type(arr))

Output

The dictionary to be converted into array: {'Age': [20, 20], 'year': [2023, 2022], 'class': [10, 9]}
The array created from the dictionary: [[  20 2023   10]
 [  20 2022    9]]
<class 'numpy.ndarray'>

Updated on: 20-Oct-2023

347 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements