Problem Formulation and Solution Overview
Example:
Input Dictionary: {2017: 74,
2018: 80,
2019: 84,
2020: 73,
2021: 79} Output NumPy Array: [[2017 74] [2018 80] [2019 84] [2020 73] [2021 79]]
The Museum of Natural History has been researching volcanic activity over a five (5) year period. They have the yearly total eruptions saved in a Dictionary format. However, they need you to convert this data into a NumPy array for analysis purposes.
Preparation
- The Pandas library enables access to/from a DataFrame.
- The NumPy library supports multi-dimensional arrays and matrices in addition to a collection of mathematical functions.
To install these libraries, navigate to an IDE terminal. At the command prompt ($
), execute the code below. For the terminal used in this example, the command prompt is a dollar sign ($
). Your terminal prompt may be different.
$ pip install pandas
Hit the <Enter>
key on the keyboard to start the installation process.
$ pip install numpy
Hit the <Enter>
key on the keyboard to start the installation process.
If the installations were successful, a message displays in the terminal indicating the same.
Feel free to view the PyCharm installation guide for the required libraries.
Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.
import pandas as pd import numpy as np
Method 1: Use NumPy Array and List
This example uses np.array()
and a list to convert the data from a Dictionary format into a NumPy array format. A great one-liner!
v_totals = {2017: 74, 2018: 80, 2019: 84, 2020: 73, 2021: 79} v_info = np.array(list(v_totals.items())) print(v_info)
This code takes the contents of v_totals
and converts this into a list, v_list
. This list then converts into a NumPy array, and the results save to v_info
. Finally, the contents are output to the terminal.
Output
[[2017 74] |
Method 2: Use np.fromiter()
In this example, the np.fromiter()
function creates two (2) new arrays from the keys and values extracted from the Dictionary format.
v_totals = {2017: 74, 2018: 80, 2019: 84, 2020: 73, 2021: 79} keys = np.fromiter(v_totals.keys(), dtype=np.int64) vals = np.fromiter(v_totals.values(), dtype=np.int64) print('Type Keys:', type(keys)) print('Type Vals:', type(vals)) print(keys, vals)
This code takes the contents of v_totals
and extracts the keys into one np.array()
and values into another np.array()
. The results save to keys
, and vals
respectively.
The following two (2) lines confirm the data was successfully converted.
Type Keys: <class 'numpy.ndarray'> |
The output from the final line is sent to the terminal.
Output
[2017 2018 2019 2020 2021] [74 80 84 73 79] |
Method 3: Use Pandas Series
According to documentation, the Pandas Series is built on top of NumPy. Therefore, applying the values method on a series will be a NumPy array.
v_totals = {2017: 74, 2018: 80, 2019: 84, 2020: 73, 2021: 79} series = pd.Series(v_totals, index=[2017, 2018, 2019, 2020, 2021]) print(series)
This code uses Pandas to take the Dictionary v_totals
, extract the values, and assign the keys as the index parameter. The result saves to series
. The output is sent to the terminal.
Output
2017 | 74 |
2018 | 80 |
2019 | 84 |
2020 | 73 |
2021 | 79 |
Method 4: Convert Nested Dictionary to NumPy array
For this example, let’s say your boss would like additional details about Volcanic eruptions for June-July, 2020. A nested dictionary below has been created to accommodate.
v_dict = {2018: 80, 2019: 84, 2020: {'June 1': 'Cleveland', 'June 16': 'Copahue', 'June 18': 'Turrialba', 'July 16': 'Indonesia', 'July 20': 'Telica'}} v_items = v_dict.items() v_data = list(v_items) results = np.array(v_data) print(results)
This code declares a nested dictionary. The items (key:value pairs) are then extracted and saved to v_items
.
This converts to a list and saves to v_data
. Next, v_data
is converted to a NumPy array and saved to results
.
Finally, the output from results
is sent to the terminal.
Output
[[2018 80] |
Summary
These four (4) methods to convert a Dictionary to a NumPy array should give you enough information to select the best one for your coding requirements.
Good Luck & Happy Coding!