Convert CSV to Dictionary in Python

Convert CSV to dictionary in Python

CSV and Dictionaries in Python

A CSV file is a comma-separated text file. It is very commonly used to transfer records and is compatible with Excel as well to store data in rows and columns. In Python, we can read CSV files easily using different functions. The pandas and csv module are used commonly to parse CSV data in Python.

A dictionary is an important data structure in Python. It can store elements in the form of key-value pairs. Every key is unique in a dictionary and can be used to access its corresponding value.

Dictionaries have a close relationship with DataFrames in Python and thus they share a similar structure to a CSV file.

Convert CSV to Dictionary in Python

A CSV file can be converted to a dictionary in several formats. We will work with the most basic format in this tutorial where we will combine two columns as key-value pairs.

We will work with the following CSV file in this tutorial.

Let us now discuss the methods to convert CSV to dictionary in Python.

Using the pandas.to_dict() function to convert CSV to dictionary in Python

The pandas module in Python works with DataFrames. A CSV file can be loaded into a DataFrame using the read_csv() function from this module.

After reading a CSV file into a DataFrame, we can convert it into a dictionary using the to_dict() function.

See the code below.

Output:

{1: 2, 3: 4, 5: 6, 7: 8}

In the above example

  • We read the CSV to a DataFrame with the read_csv() function.
  • The header parameter specifies that there is no header file and the index_col parameter specifies the column for the index in the CSV.
  • The squeeze parameter returns a Series object if only one column is parsed.
  • We convert this DataFrame to a dictionary using the to_dict() function and display it.

Using the csv module to convert CSV to dictionary in Python

We can read and work with CSV files in Python using the csv module. We will read a given file and parse the data through the reader class of this module.

After parsing the data, we will run a for loop to iterate this data and create a dictionary using dictionary comprehension. Dictionary comprehension is an elegant way to create a dictionary in a single line of code using the for loop.

See the code below.

Output:

{‘1’: ‘2’, ‘3’: ‘4’, ‘5’: ‘6’, ‘7’: ‘8’}

In the above example,

  • We read a CSV file in the read mode using the open() function.
  • Data is parsed using the csv.reader() function.
  • We loop over and add the columns of a row as key-value pairs in a dictionary using the for loop.

Using the filter() function to convert CSV to dictionary in Python

This method is similar to the previous one. We will similarly use the csv module to read and parse through the CSV data.

The filter() function returns an iterable after applying a function to every element and removing the data fail to satisfy the condition in the function. We just use it to create an iterable and convert it into a dictionary using the dict() constructor.

See the code below.

Output:

{‘1’: ‘2’, ‘3’: ‘4’, ‘5’: ‘6’, ‘7’: ‘8’}

Using the numpy.loadtxt() function to convert CSV to dictionary in Python

In this method, we will load the CSV file using the numpy.loadtxt() function to a numpy array. We will then traverse through this array to create a dictionary as we did previously using the for loop.

See the code below.

Output:

{1.0: 2.0, 3.0: 4.0, 5.0: 6.0, 7.0: 8.0}

This method works only with a CSV file that contains integers and float values. This is because every value is converted to a float while reading with the numpy.loadtxt() function.

Convert CSV to List of Dictionaries

As discussed earlier, after converting the CSV to a dictionary, the final format may vary for the user. We will try to show how to convert CSV to list of dictionaries in Python.

We will work with the same dictionary.

Using the pandas.to_dict() function to convert CSV to list of dictionaries in Python

As discussed earlier, we can load a CSV file to a DataFrame and convert it into a dictionary using the to_dict() function.

This function accepts different formats to define the final structure of the dictionary. By default, it is of the format dict.

To convert CSV to list of dictionaries, we will change the format to records. This format stores the DataFrame as a list of dictionaries.

For example,

Output:

[{1: 2}, {1: 4}, {1: 6}, {1: 8}]

In the above example,

  • We do not set the squeeze parameter while reading the CSV file into a DataFrame
  • The format is specified in the to_dict() function.
  • The final result is a list of dictionaries.

Using the csv.DictReader class to convert CSV to list of dictionaries

As discussed we can work with CSV files with the csv module. The DictReader class maps the CSV data to a dictionary. We can individually append the dictionaries from this data to a list to get a list of dictionaries.

See the code below.

Output:

[{‘1’: ‘3’, ‘2’: ‘4’}, {‘1’: ‘5’, ‘2’: ‘6’}, {‘1’: ‘7’, ‘2’: ‘8’}]
  • We parse the data using the DictReader class.
  • Each row of this parsed data is of OrderedDict class
  • Each row of parsed data is converted to a dictionary using the dict() constructor.
  • We append these dictionaries to a list and display it.

Using the csv.reader class to convert CSV to list of dictionaries in Python

In the previous section, we discovered how to convert CSV to dictionary in Python using the csv.reader class. We can also use the parsed data from this object and return a list of dictionaries.

We use the map() function and convert every row into an integer with the int() function. These rows are individually combined with the first row using the zip() function.

These objects are converted to a dictionary using the dict() and are appended to a list.

We implement this in the following example.

Output:

[{‘1’: 3, ‘2’: 4}, {‘1’: 5, ‘2’: 6}, {‘1’: 7, ‘2’: 8}]

In the above example,

  • We parse the CSV data using the csv.reader class.
  • We store the first row in a variable using the next() function.
  • The zip() method combines two iterable objects to return a zip-type object.
  • It combines the elements and we convert this to a dictionary with the dict() function.
  • The dictionary is added to the list.

Conclusion

We discussed different methods to convert CSV to dictionary in Python. We discussed the basic structure of both these objects and how the final format may be different in different ways. The csv module was used to parse the CSV data and convert it to a dictionary. We could also use the filter() function with this method. The pandas module provides a direct method to read CSV and convert it into a dictionary with the to_dict() function. We also used the numpy.loadtxt() method for the same.

We also discussed how to convert CSV to list of dictionaries. The same pandas.to_dict() method can be used for this but we specify the format of the final dictionary as records. The csv.DictReader can parse the csv to dictionaries which can be appended to a list. We can also use the csv.reader class for this, but we have to perform some complex operations to convert this data to a dictionary and append them to a list.

That’s all about how to convert CSV to dictionary in Python.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *