Preparation
Before any data manipulation can occur, two (2) new libraries will require installation.
- 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
DataFrame.to_dict()
The to_dict()
method converts a valid DataFrame structure to a dictionary format.
The syntax for this method is as follows:
DataFrame.to_dict(orient='dict', into=<class 'dict'>)
Parameter | Description |
---|---|
orient | This parameter sets the values of the dictionary. The available options are: – 'dict' : dictionary: {column -> {index -> value}} – ‘list’ : dictionary: {column -> [values]} – ‘series’ : dictionary: {column -> Series(values)} – ‘split’ : dictionary: {‘index’ -> [index], ‘columns’, etc.} – ‘tight’ : dictionary: {‘index’ -> [index], etc.} – ‘records’ : list: [{column -> value}, … , {column -> value}] – ‘index’ : dictionary: {index -> {column -> value}} |
into | This parameter sets the data structure to convert the data into. The default value is a dictionary. |
This example reads in the file’s first (5) rows / three (3) columns to a DataFrame. This DataFrame then converts to a dictionary format.
Click here to save this CSV file and move it to the current working directory.
df = pd.read_csv('finxters.csv', usecols=['FID', 'First_Name', 'Last_Name']).head() print(df) result = df.to_dict() print(result)
- Line [1] reads in the first five (5) rows (head) and three (3) columns (usecols) of the
finxters.csv
file. The output saves to a DataFrame (df
). - Line [2] outputs the DataFrame to the terminal.
- Line [3] converts the DataFrame (
df
) to a dictionary. The output saves toresult
. - Line [4] outputs the result to the terminal.
Output – df
FID | First_Name | Last_Name | |
0 | 30022145 | Steve | Hamilton |
1 | 30022192 | Amy | Pullister |
2 | 30022331 | Peter | Dunn |
3 | 30022345 | Marcus | Williams |
4 | 30022359 | Alice | Miller |
Output – result
{'FID': {0: 30022145, 1: 30022192, 2: 30022331, 3: 30022345, 4: 30022359}, |
If the split
parameter was passed to the to_dict()
parameter, the output would be as follows:
df = pd.read_csv('finxters.csv', usecols=['FID', 'First_Name', 'Last_Name']).head() print(df) result = df.to_dict('split') print(result)
Output – result
{'index': [0, 1, 2, 3, 4], |
More Pandas DataFrame Methods
Feel free to learn more about the previous and next pandas DataFrame methods (alphabetically) here:
Also, check out the full cheat sheet overview of all Pandas DataFrame methods.