Pandas: Create a Dictionary from two DataFrame Columns

avatar
Borislav Hadzhiev

Last updated: Apr 12, 2024
4 min

banner

# Table of Contents

  1. Pandas: Create a Dictionary from two DataFrame Columns
  2. Pandas: Create a Dictionary from two DataFrame Columns using dict()
  3. Pandas: Create a Dictionary from two DataFrame Columns using set_index()
  4. Pandas: Create a Dictionary from two DataFrame Columns using to_records()
  5. Pandas: Create a Dictionary from two DataFrame Columns using MultiIndex.from_frame

# Pandas: Create a Dictionary from two DataFrame Columns

To create a dictionary from two DataFrame columns in Pandas:

  1. Use the pandas.Series() constructor to create a Series.
  2. Use an array of the values of the first column for the data argument.
  3. Use the values in the second column for the index argument.
main.py
import pandas as pd df = pd.DataFrame({ 'digit': [1, 2, 3], 'day_name': ['Monday', 'Tuesday', 'Wednesday'] }) print(df) print('-' * 50) a_dict = pd.Series( df['day_name'].values, index=df['digit'] ).to_dict() print(a_dict)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
digit day_name 0 1 Monday 1 2 Tuesday 2 3 Wednesday -------------------------------------------------- {1: 'Monday', 2: 'Tuesday', 3: 'Wednesday'}

create dictionary from two dataframe columns in pandas

The pandas.Series class is used to create a one-dimensional ndarray with axis labels.

main.py
# {1: 'Monday', 2: 'Tuesday', 3: 'Wednesday'} a_dict = pd.Series( df['day_name'].values, index=df['digit'] ).to_dict()

We used the DataFrame.values attribute to get a NumPy representation of the day_name column.

main.py
import pandas as pd df = pd.DataFrame({ 'digit': [1, 2, 3], 'day_name': ['Monday', 'Tuesday', 'Wednesday'] }) # ['Monday' 'Tuesday' 'Wednesday'] print(df['day_name'].values)

The digit column is used for the index parameter of the pandas.Series class.

main.py
import pandas as pd df = pd.DataFrame({ 'digit': [1, 2, 3], 'day_name': ['Monday', 'Tuesday', 'Wednesday'] }) # 0 1 # 1 2 # 2 3 # Name: digit, dtype: int64 print(df['digit'])
The code for this article is available on GitHub

The last step is to use the Series.to_dict() method to convert the Series to a dictionary.

main.py
# {1: 'Monday', 2: 'Tuesday', 3: 'Wednesday'} a_dict = pd.Series( df['day_name'].values, index=df['digit'] ).to_dict()

The to_dict() method returns the key-value representation of the Series.

# Pandas: Create a Dictionary from two DataFrame Columns using dict()

You can also use the dict() class and the zip function to create a dictionary from two DataFrame columns.

main.py
import pandas as pd df = pd.DataFrame({ 'digit': [1, 2, 3], 'day_name': ['Monday', 'Tuesday', 'Wednesday'] }) print(df) print('-' * 50) a_dict = dict(zip(df['digit'], df['day_name'])) print(a_dict)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
digit day_name 0 1 Monday 1 2 Tuesday 2 3 Wednesday -------------------------------------------------- {1: 'Monday', 2: 'Tuesday', 3: 'Wednesday'}

create dictionary from two dataframe columns using dict

The zip() function iterates over several iterables in parallel and produces tuples with an item from each iterable.

main.py
import pandas as pd df = pd.DataFrame({ 'digit': [1, 2, 3], 'day_name': ['Monday', 'Tuesday', 'Wednesday'] }) # [(1, 'Monday'), (2, 'Tuesday'), (3, 'Wednesday')] print(list(zip(df['digit'], df['day_name'])))
The code for this article is available on GitHub

We can pass the iterable of tuples to the dict() class to construct a dictionary.

If you'd like to swap the keys and values, simply switch the places when calling zip().

main.py
import pandas as pd df = pd.DataFrame({ 'digit': [1, 2, 3], 'day_name': ['Monday', 'Tuesday', 'Wednesday'] }) a_dict = dict(zip(df['day_name'], df['digit'])) # {'Monday': 1, 'Tuesday': 2, 'Wednesday': 3} print(a_dict)

# Pandas: Create a Dictionary from two DataFrame Columns using set_index()

You can also use the DataFrame.set_index() method to create a dictionary from two DataFrame columns.

main.py
import pandas as pd df = pd.DataFrame({ 'digit': [1, 2, 3], 'day_name': ['Monday', 'Tuesday', 'Wednesday'] }) a_dict = df.set_index('digit').to_dict()['day_name'] # {'Monday': 1, 'Tuesday': 2, 'Wednesday': 3} print(a_dict)

create dictionary from two dataframe columns using set index

The code for this article is available on GitHub

The DataFrame.set_index() method sets the DataFrame index using the specified column.

main.py
import pandas as pd df = pd.DataFrame({ 'digit': [1, 2, 3], 'day_name': ['Monday', 'Tuesday', 'Wednesday'] }) # day_name # digit # 1 Monday # 2 Tuesday # 3 Wednesday print(df.set_index('digit'))

The method returns a DataFrame on which we can access the to_dict() method.

main.py
import pandas as pd df = pd.DataFrame({ 'digit': [1, 2, 3], 'day_name': ['Monday', 'Tuesday', 'Wednesday'] }) # {'day_name': {1: 'Monday', 2: 'Tuesday', 3: 'Wednesday'}} print(df.set_index('digit').to_dict())
The code for this article is available on GitHub

The last step is to access the day_name key to get the nested dictionary.

main.py
import pandas as pd df = pd.DataFrame({ 'digit': [1, 2, 3], 'day_name': ['Monday', 'Tuesday', 'Wednesday'] }) # {'day_name': {1: 'Monday', 2: 'Tuesday', 3: 'Wednesday'}} print(df.set_index('digit').to_dict()) # {1: 'Monday', 2: 'Tuesday', 3: 'Wednesday'} print(df.set_index('digit').to_dict()['day_name'])

# Pandas: Create a Dictionary from two DataFrame Columns using to_records()

You can also use the DataFrame.to_records method if your DataFrame only has 2 columns.

main.py
import pandas as pd df = pd.DataFrame({ 'digit': [1, 2, 3], 'day_name': ['Monday', 'Tuesday', 'Wednesday'] }) a_dict = dict(df.to_records(index=False)) # {1: 'Monday', 2: 'Tuesday', 3: 'Wednesday'} print(a_dict)

create dictionary from two dataframe columns using to records

The code for this article is available on GitHub

The DataFrame.to_records() method converts the DataFrame to a NumPy record array.

main.py
import pandas as pd df = pd.DataFrame({ 'digit': [1, 2, 3], 'day_name': ['Monday', 'Tuesday', 'Wednesday'] }) # [(0, 1, 'Monday') (1, 2, 'Tuesday') (2, 3, 'Wednesday')] print(df.to_records()) # [(1, 'Monday') (2, 'Tuesday') (3, 'Wednesday')] print(df.to_records(index=False))

We had to set the index argument to False to exclude the index from the resulting array of tuples.

You can pass the array of tuples to the dict() class to create a dictionary.

# Pandas: Create a Dictionary from two DataFrame Columns using MultiIndex.from_frame

You can also use the MultiIndex.from_frame() method.

main.py
import pandas as pd df = pd.DataFrame({ 'digit': [1, 2, 3], 'day_name': ['Monday', 'Tuesday', 'Wednesday'] }) a_dict = dict(pd.MultiIndex.from_frame(df)) # {1: 'Monday', 2: 'Tuesday', 3: 'Wednesday'} print(a_dict)

create dictionary from two dataframe columns using multi index from frame

The code for this article is available on GitHub

The pandas.MultiIndex.from_frame method creates a MultiIndex from a DataFrame.

main.py
import pandas as pd df = pd.DataFrame({ 'digit': [1, 2, 3], 'day_name': ['Monday', 'Tuesday', 'Wednesday'] }) # MultiIndex([(1, 'Monday'), # (2, 'Tuesday'), # (3, 'Wednesday')], # names=['digit', 'day_name']) print(pd.MultiIndex.from_frame(df))

You can pass the MultiIndex to the dict() class to construct a dictionary.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.