> ## Content Index
> Fetch the complete content index at: https://datascientyst.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# How to Create DataFrame from Dictionary in Pandas?
- URL: https://datascientyst.com/create-dataframe-from-dictionary-pandas/
- Published: 2022-01-30T08:58:54.000Z
- Updated: 2022-01-30T08:58:54.000Z
- Author: John D K
- Tags: Create

## 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**

```python
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**

```python
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()](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.from%5Fdict.html?ref=datascientyst.com) to create DataFrame from dictionary has several options:

- default behavior - the keys of the dict will be the columns
- `orient='index'` \- dictionary keys as rows
- `orient='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:

```python
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:

```python
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:**

```python
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:

```python
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:

```python
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:

```python
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.