> ## 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 Extract Dictionary Value from Column in Pandas
- URL: https://datascientyst.com/extract-dictionary-value-from-column-in-pandas/
- Published: 2023-03-18T09:44:49.000Z
- Updated: 2023-03-18T09:44:49.000Z
- Author: John D K
- Tags: Convert

In this short guide, I'll show you how to **extract or explode a dictionary value from a column in a Pandas DataFrame**. You can use:

- list or dict comprehension to extract dictionary values
- the `apply()` function along with a lambda function to extract the value from each dictionary

![](https://datascientyst.com/content/images/2023/03/extract-dictionary-value-from-column-in-dataframe.webp)

## Setup

For example, suppose you have a DataFrame with a column containing dictionaries:

```python
import pandas as pd

data = {'data': [{'a': 1, 'b': 2}, {'a': 3, 'b': 4}]}
df = pd.DataFrame(data)

```

|   | data             |
| - | ---------------- |
| 0 | {'a': 1, 'b': 2} |
| 1 | {'a': 3, 'b': 4} |

## Extract key from dict column

To extract values for a particular key from the dictionary in each row, we can use the following code:

```python
df['key'] = df['data'].apply(lambda x: x[key])

```

which give us a Series of all values matching this key:

```
0	1
1	3
Name: data, dtype: int64

```

Finally we create a new column from the extracted data.

As we mentioned at the start we can use list comprehension to extract all values to list:

```python
[d.get('a') for d in df.data]

```

which give us:

```
[1, 3]

```

## Split/Explode a column of dictionaries

To split or explode a column of dictionaries to separate columns we can use: `.apply(pd.Series)`:

```python
df['data'].apply(pd.Series)

```

this give us new DataFrame with columns from the exploded dictionaries:

|   | a | b |
| - | - | - |
| 0 | 1 | 2 |
| 1 | 3 | 4 |

A faster way to achieve similar behavior is by using `pd.json_normalize(df['data'])`:

```python
pd.json_normalize(df['data'])

```

For more information and examples you can check:

- [How to Normalize JSON or Dict to New Columns in Pandas ](https://datascientyst.com/normalize-json-dict-new-columns-pandas/)

## Extract all values/keys from dict column

To extract all keys and values from a column which contains dictionary data we can list all keys by: `list(x.keys())`. Below are several examples:

```python
df['keys'] = df['data'].apply(lambda x: list(x.keys()))
df['values'] = df['data'].apply(lambda x: list(x.values()))

```

which create new column only with the keys or the values from the original dictionary:

|   | data             | keys     | values   |
| - | ---------------- | -------- | -------- |
| 0 | {'a': 1, 'b': 2} | \[a, b\] | \[1, 2\] |
| 1 | {'a': 3, 'b': 4} | \[a, b\] | \[3, 4\] |

## Summary

In this article, we looked at different solutions for extraction and explosion of dictionary columns in Pandas. We focused on extraction in these code snippets, but exploding and splitting is very similar.