In this short guide, I'll show you how to convert Pandas DataFrame to dictionary. You can also find how to use Pandas method - to_dict()
. So at the end you will get from DataFrame to Python dict:
day | numeric | |
---|---|---|
0 | 1 | 1 |
1 | 2 | 2 |
2 | 3 | 3 |
3 | 4 | 4 |
4 | 5 | 5 |
to:
{'day': {0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6},
'numeric': {0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6}}
or any other dict-like format. We will also cover the following examples:
dict
(default) : dict like {column -> {index -> value}}list
: dict like {column -> [values]}series
: dict like {column -> Series(values)}split
: dict like -{'index' -> [index], 'columns' -> [columns], 'data' -> [values]}
tight
: dict like
{'index' -> [index], 'columns' -> [columns], 'data' -> [values], 'index_names' -> [index.names], 'column_names' -> [column.names]}
records
: list like -[{column -> value}, ... , {column -> value}]
index
: dict like {index -> {column -> value}}
To start, here is the syntax that you may apply in order to convert DataFrame to dict:
df.to_dict()
In the next section, I'll review the steps to apply the above syntax in practice.
Step 1: Create a DataFrame
Lets create a DataFrame which has a two columns:
import pandas as pd
data={'day': [1, 2, 3, 4, 5, 6],
'numeric': [1, 2, 3, 4, 5, 6]}
df = pd.DataFrame(data)
df
result:
day | numeric | |
---|---|---|
0 | 1 | 1 |
1 | 2 | 2 |
2 | 3 | 3 |
3 | 4 | 4 |
4 | 5 | 5 |
Step 2: DataFrame to dict - {column -> {index -> value}}
In order to extract DataFrame as Python dictionary we need just this line:
df.to_dict()
result:
{'day': {0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6},
'numeric': {0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6}}
By default method to_dict()
use as parameter - orient='list'
and will produce dict form of:
{column -> {index -> value}}
Step 3: DataFrame to dict - list - {column -> [values]}
What if you like to get a dictionary only with the values? In this case we will use orient='list'
in order to exclude index from the output dictionary:
df.to_dict(orient='list')
{'day': [1, 2, 3, 4, 5, 6], 'numeric': [1, 2, 3, 4, 5, 6]}
Note: have in mind that there is a difference between Python dict and JSON format.
Step 3: split - {'index' -> [index], 'columns' -> [columns], 'data' -> [values]}
Another form of dictionary which includes:
- index
- columns
- data
as separate lists can be extracted by:
df.to_dict(orient='split')
{'index': [0, 1, 2, 3, 4, 5],
'columns': ['day', 'numeric'],
'data': [[1, 1], [2, 2], [3, 3], [4, 4], [5, 5], [6, 6]]}
and then:
df['yyyy'].astype(str) + '-'+ df['mm'].astype(str)
Step 4: Convert DataFrame to dict - tight format
If you like to get a tight format of DataFrame as dict we can use the parameter orient='tight'
.
This option includes the label names - index and column names in the output dict:
df.to_dict(orient='tight')
to get:
{'index': [0, 1, 2, 3, 4, 5],
'columns': ['day', 'numeric'],
'data': [[1, 1], [2, 2], [3, 3], [4, 4], [5, 5], [6, 6]],
'index_names': [None],
'column_names': [None]}
Step 5: Convert DataFrame to dict - records
If we like to extract only the values and the column labels as dictionary we can do it by - orient='records'
:
df.to_dict(orient='records')
We will get the list of dicts from the original DataFrame:
[{'day': 1, 'numeric': 1},
{'day': 2, 'numeric': 2},
{'day': 3, 'numeric': 3},
{'day': 4, 'numeric': 4},
{'day': 5, 'numeric': 5},
{'day': 6, 'numeric': 6}]
Step 6: Convert DataFrame to dict - index
Finally we can convert DataFrame to dictionary with index by:
df.to_dict(orient='index')
result:
{0: {'day': 1, 'numeric': 1},
1: {'day': 2, 'numeric': 2},
2: {'day': 3, 'numeric': 3},
3: {'day': 4, 'numeric': 4},
4: {'day': 5, 'numeric': 5},
5: {'day': 6, 'numeric': 6}}
More information about method - to_dict