How to Convert Pandas Dataframe to Dictionary
-
Pandas DataFrame to Dictionary Using
to_dict()
Function -
Pandas DataFrame to Dictionary With Values as List or
Series
- Pandas DataFrame to List of Dictionaries
- Pandas Dataframe to Dictionary by Rows
-
Dataframe to Dictionary With One Column as
key
-
Pandas DataFrame to Dictionary Using
dict()
andzip()
Functions
This tutorial will introduce how to convert a Pandas DataFrame to a dictionary with the index column elements as the key and the corresponding elements at other columns as the value. We will use the following DataFrame in the article.
import pandas as pd
df = pd.DataFrame(
[["Jay", 16, "BBA"], ["Jack", 19, "BTech"], ["Mark", 18, "BSc"]],
columns=["Name", "Age", "Course"],
)
print(df)
Output:
Name Age Course
0 Jay 16 BBA
1 Jack 19 BTech
2 Mark 18 BSc
Pandas DataFrame to Dictionary Using to_dict()
Function
Pandas to_dict()
function converts a DataFrame to a dictionary. The parameters determine the format of the dictionary and how the key-value pairs are associated. An elementary example of converting a DataFrame to Dictionary using to_dict()
is shown below:
import pandas as pd
df = pd.DataFrame(
[["Jay", 16, "BBA"], ["Jack", 19, "BTech"], ["Mark", 18, "BSc"]],
columns=["Name", "Age", "Course"],
)
d1 = df.to_dict()
print(d1)
Output:
{'Name': {0: 'Jay', 1: 'Jack', 2: 'Mark'}, 'Age': {0: 16, 1: 19, 2: 18}, 'Course': {0: 'BBA', 1: 'BTech', 2: 'BSc'}}
As you can see in the output, column names get converted to keys and each record as the value, with index as their key.
Pandas DataFrame to Dictionary With Values as List or Series
We can pass parameters as list
, records
, series
, index
, split
, and dict
to to_dict()
function to alter the format of the final dictionary. For example, when we pass list
and series
as the parameter, we have the column names as the keys, but the value pairs get converted to a list and series of rows, respectively. The following example will demonstrate this.
import pandas as pd
df = pd.DataFrame(
[["Jay", 16, "BBA"], ["Jack", 19, "BTech"], ["Mark", 18, "BSc"]],
columns=["Name", "Age", "Course"],
)
d_list = df.to_dict("list")
print(d_list)
d_series = df.to_dict("series")
print(d_series)
Output:
{'Name': ['Jay', 'Jack', 'Mark'], 'Age': [16, 19, 18], 'Course': ['BBA', 'BTech', 'BSc']}
{'Name': 0 Jay
1 Jack
2 Mark
Name: Name, dtype: object, 'Age': 0 16
1 19
2 18
Name: Age, dtype: int64, 'Course': 0 BBA
1 BTech
2 BSc
Name: Course, dtype: object}
Pandas DataFrame to List of Dictionaries
We can also have each row as a separate dictionary be passing records
to the function. The final result is a list with each row as a dictionary. For example,
import pandas as pd
df = pd.DataFrame(
[["Jay", 16, "BBA"], ["Jack", 19, "BTech"], ["Mark", 18, "BSc"]],
columns=["Name", "Age", "Course"],
)
d_records = df.to_dict("records")
print(d_records)
Output:
[{'Name': 'Jay', 'Age': 16, 'Course': 'BBA'}, {'Name': 'Jack', 'Age': 19, 'Course': 'BTech'}, {'Name': 'Mark', 'Age': 18, 'Course': 'BSc'}]
Pandas Dataframe to Dictionary by Rows
But for many cases, we may not want the column names as the keys to the dictionary. For such situations, we can pass index
to make the DataFrame index as keys. The following code snippet will show it.
import pandas as pd
df = pd.DataFrame(
[["Jay", 16, "BBA"], ["Jack", 19, "BTech"], ["Mark", 18, "BSc"]],
columns=["Name", "Age", "Course"],
)
d_index = df.to_dict("index")
print(d_index)
Output:
{0: {'Name': 'Jay', 'Age': 16, 'Course': 'BBA'}, 1: {'Name': 'Jack', 'Age': 19, 'Course': 'BTech'}, 2: {'Name': 'Mark', 'Age': 18, 'Course': 'BSc'}}
Dataframe to Dictionary With One Column as key
But what if we prefer to use the elements of one column as the keys and the elements at other columns as the values? It is possible by simply making the required column as the index of the DataFrame and taking its transpose using .T()
function.
Example:
import pandas as pd
df = pd.DataFrame(
[["Jay", 16, "BBA"], ["Jack", 19, "BTech"], ["Mark", 18, "BSc"]],
columns=["Name", "Age", "Course"],
)
d_names = df.set_index("Name").T.to_dict("list")
print(d_names)
Output:
{'Jay': [16, 'BBA'], 'Jack': [19, 'BTech'], 'Mark': [18, 'BSc']}
Pandas DataFrame to Dictionary Using dict()
and zip()
Functions
Python dict()
function can also convert the Pandas DataFrame to a dictionary. We should also use the zip()
function with the individual columns as the arguments in it to create the parallel iterator. Then the zip()
function will yield all the values in one row in each iteration.
import pandas as pd
df = pd.DataFrame(
[["Jay", 16, "BBA"], ["Jack", 19, "BTech"], ["Mark", 18, "BSc"]],
columns=["Name", "Age", "Course"],
)
d = dict([(i, [a, b]) for i, a, b in zip(df["Name"], df["Age"], df["Course"])])
print(d)
Output:
{'Jay': [16, 'BBA'], 'Jack': [19, 'BTech'], 'Mark': [18, 'BSc']}
Manav is a IT Professional who has a lot of experience as a core developer in many live projects. He is an avid learner who enjoys learning new things and sharing his findings whenever possible.
LinkedIn