How to read a CSV to the dictionary using Pandas in Python [3 Examples]

Do you want to read CSV to the dictionary through Pandas? In this Pandas tutorial, I will explain how to read a CSV to the dictionary using Pandas in Python using different methods with examples.

To read a CSV to a dictionary using Pandas in Python, we can first use read_csv to import the file into a DataFrame, then apply to_dict(). This method can be customized for specific data structures or used with the ‘records’ orientation to create a list of dictionaries, each representing a row, enabling flexible and tailored data manipulation.

Read a CSV to the dictionary using Pandas in Python

To read a CSV to the dictionary using Pandas in Python, we can use the read_csv function from the Pandas library. It is used to read a CSV file as a dataframe in Python.

Once we have the CSV data in a DataFrame, we can convert it to a dictionary. Pandas provides several methods to do this. The most common method is to_dict(), which converts the DataFrame into a dictionary in Python.

Let’s see some examples to do so:

Note: We can use the type() function we confirm that Output class.

1. Pandas csv to dictionary using read_csv with to_dict function

By default, the to_dict() function in Python converts the DataFrame into a dictionary of series. In this format, each column becomes a key in the dictionary, and the values are lists of data in that column.

Here is the code to read a CSV to the dictionary using Pandas in Python:

import pandas as pd

df = pd.read_csv('us_states_population.csv')
data_dict = df.to_dict()

print(data_dict)
print(type(data_dict))

Output:

{'State': {0: 'California', 1: 'Texas', 2: 'Florida'}, 'Population': {0: 39538223, 1: 29145505, 2: 21538187}}
<class 'dict'>

The output from the code executed in PyCharm can be seen in the screenshot provided below.

How to read a CSV to the dictionary using Pandas in Python

2. Python Pandas read csv to dictionary with record-oriented

If we prefer a list of dictionaries where each dictionary represents a row, use the ‘records‘ orientation. Each dictionary in the list represents a row, with keys as column headers and values as row data.

Here is the code to read a CSV to the dictionary using Pandas in Python:

import pandas as pd

df = pd.read_csv('usa_universities.csv')
data_dict = df.to_dict(orient='records')

for data in data_dict:
        print(data)
        print(type(data))

Output:

{'Name': 'Harvard University', 'State': 'Massachusetts', 'FoundedYear': 1636}
<class 'dict'>
{'Name': 'Stanford University', 'State': 'California', 'FoundedYear': 1885}
<class 'dict'>
{'Name': 'Massachusetts Institute of Technology', 'State': 'Massachusetts', 'FoundedYear': 1861}
<class 'dict'>

The following screenshot illustrates the result after running the code in the PyCharm editor.

csv to dictionary python pandas

3. Pandas read csv to dictionary with customized data

We can also customize the dictionary conversion based on our needs.

For example, if we want to use one of the columns as keys and another column as values, we can do something like this:

import pandas as pd

df = pd.read_csv('us_presidents.csv')
data_dict = df.set_index('President')['StateBornIn'].to_dict()

print(data_dict)
print(type(data_dict))

Output: This will create a dictionary where keys are from the first column and values are from the other column.

{'George Washington': 'Virginia', 'John Adams': 'Massachusetts', 'Thomas Jefferson': 'Virginia', 'James Madison': 'Virginia'}
<class 'dict'>

Below is a screenshot showing the output after the code was executed in the PyCharm editor.

pandas read csv as dictionary in Python

Conclusion

Here, I have explained how to read a CSV file into a dictionary using Pandas in Python, showcasing three distinct approaches of using the read_csv with to_dict() method: the direct approach that maps columns to lists, the record-oriented method for row-based dictionaries, and a customized approach for tailored key-value pairs.

These examples demonstrate the flexibility and efficiency of Pandas in handling and transforming CSV data to suit diverse data processing needs.

You may also like to read: