1. Overview
To create DataFrame from dictionary in Pandas there are several options depending on the dictionary and desired result:
(1) create DataFrame from dictionary using default Constructor
data = {'Member': {0: 'John', 1: 'Bill', 2: 'Jim'},
'Disqualified': {0: 0, 1: 1, 2: 0},
'Paid': {0: 1, 1: 0, 2: 3}}
df = pd.DataFrame(data)
result:
Member | Disqualified | Paid | |
---|---|---|---|
0 | John | 0 | 1.0 |
1 | Bill | 1 | 0.0 |
2 | Jim | 0 | 3.0 |
(2) use method from_dict() to create DataFrame from dictionary
import pandas as pd
data = {'col_1': [1, 2, 3], 'col_2': ['x', 'y', 'z']}
pd.DataFrame.from_dict(data)
result:
col_1 | col_2 | |
---|---|---|
0 | 1 | x |
1 | 2 | y |
2 | 3 | z |
Let's cover those ways in more detail in the next steps.
2. Use method from_dict() to create DataFrame from dictionary
Explicitly using the method from_dict() to create DataFrame from dictionary has several options:
- default behavior - the keys of the dict will be the columns
orient='index'
- dictionary keys as rowsorient='tight'
- using a tight format
2.1. Pandas from_dict() - default behavior
By default the method from_dict()
will use the keys of the dictionary as column names. The dictionary values will be used as a DataFrame values:
import pandas as pd
data = {'col_1': [1, 2, 3], 'col_2': ['x', 'y', 'z']}
df = pd.DataFrame.from_dict(data)
result:
col_1 | col_2 | |
---|---|---|
0 | 1 | x |
1 | 2 | y |
2 | 3 | z |
2.2. Pandas from_dict() - dictionary keys as rows
If you like to use dict keys as a rows you can specify parameter orient='index'
- this will create DataFrame with values from the dict values:
import pandas as pd
data = {'row_1': [1, 2, 3], 'row_2': ['x', 'y', 'z']}
df = pd.DataFrame.from_dict(data, orient='index')
the new DataFrame will looks like:
0 | 1 | 2 | |
---|---|---|---|
row_1 | 1 | 2 | 3 |
row_2 | x | y | z |
2.3. Pandas from_dict() - using a tight format
The final option is to use tight format - orient='tight'
. This option is available only with version 1.4 - otherwise error will be raised:
ValueError: only recognize index or columns for orient
It can used to create MultiIndex DataFrames from dictionary like:
data = {'index': [('row_1', 'row_21'), ('row_1', 'row_22')],
'columns': [('col_11', 'col_21'), ('col_12', 'col_22')],
'data': [['val_11', 'val_21'], ['val_12', 'val_22']],
'index_names': ['ix level 1', 'ix level 2'],
'column_names': ['col level 1', 'col level 2']}
pd.DataFrame.from_dict(data, orient='tight')
result:
col level 1 | col_11 | col_12 | |
---|---|---|---|
col level 2 | col_21 | col_22 | |
ix level 1 | ix level 2 | ||
row_1 | row_21 | val_11 | val_21 |
row_22 | val_12 | val_22 |
3. Create DataFrame from dictionary using default Constructor
One more way to create DataFrame from dicts is by using the Constructor and providing the dictionary to it. There are several different options:
- Dict with list values - dict keys as columns and dict values as data
- default index
- explicit index
- Nested dictionary to create DataFrame - define index for the rows
3.1. Dict with list values
If the input dict has list values than they will be used as values for the DataFrame:
data = {'Member': ['John', 'Bill', 'Jim'],
'Disqualified': [0,1,2]}
df = pd.DataFrame(data)
the output is:
Member | Disqualified | |
---|---|---|
0 | John | 0 |
1 | Bill | 1 |
2 | Jim | 2 |
To define index in this case we can pass it to the parameter by:
pd.DataFrame(data, index = ['x', 'y', 'z'])
3.2. Nested dictionary to create DataFrame
When we are using nested dictionary we will use index:value pairs for the dict:
details = {
'col_1' : {
'row_1' : 'x',
'row_2' : 'y',
'row_3' : 'z'
},
'col_2' : {
'row_1' : 1,
'row_2' : 2,
'row_3' : 3
}
}
df = pd.DataFrame(details)
result:
col_1 | col_2 | |
---|---|---|
row_1 | x | 1 |
row_2 | y | 2 |
row_3 | z | 3 |
4. Conclusion
To summarize, in this article,** we've seen examples of creating DataFrame from dictionary in multiple ways**. We've briefly discussed these creation methods.
And finally, we've seen how to create a MultiIndex DataFrame from a dict.