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.


import_contacts

You may also like:

Related Posts

  • 12 May

    Extract Integer from String in Python

    Table of ContentsHow to extract integer from string in Python?Using the isdigit() function to extract integer from string in Python.Using the filter() function to extract integer from string in PythonUsing the re.search() function to extract integer from string in PythonConclusion How to extract integer from string in Python? A string can store a sequence of […]

  • Make requirements.txt in Python
    03 May

    Make requirements.txt in python

    Table of ContentsWhat is the requirements.txt file in Python?Ways to make requirements.txt file in PythonUsing the pip to make requirements.txt in PythonUsing the conda command to make requirements.txt file in PythonUsing the pipreqs package to make requirements.txt file in PythonConclusion What is the requirements.txt file in Python? Every package is also updated regularly and features […]

  • Create an array of 1 to 10 in Python
    03 May

    Create an Array of 1 to 10 in Python

    Table of ContentsIntroductionHow to create an array of 1 to 10 in Python?Using the range() function to create an array of 1 to 10 in Python.Using list comprehension along with the range() function to create an array of 1 to 10 in Python.Using a user-defined function to create an array of 1 to 10 in […]

  • Remove Urls from String in Python
    03 May

    Remove Urls from Text in Python

    Table of ContentsIntroductionWays to remove URLs from Text in PythonUsing the re.sub() function to remove URLs from Text in PythonUsing the re.findall() function to remove URLs from Text in PythonUsing the re.search() function to remove URLs from Text in PythonUsing the urllib.urlparse class to remove URLs from Text in PythonConclusion In this post, we will […]

  • Create a list from 1 to 100 in Python
    03 May

    Create a List from 1 to 100 in Python

    Table of ContentsWays to create a list from 1 to 100 in PythonUsing the range() function to create a list from 1 to 100 in PythonUsing the numpy.arange() function to create a list from 1 to 100 in PythonUsing the for loop with range() to create a list from 1 to 100 in PythonConclusion In […]

  • Return vs Print in Python
    16 April

    Return vs Print in Python

    Table of ContentsReturn vs Print in PythonConclusion Return vs Print in Python In Python, we work with a wide range of functions that make our code simple. We can also create functions using the def keyword. A function can be defined as per our requirement and can display something or return a value. This is […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.