Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 22462

Python for Beginners: List of Dictionaries to Dataframe in Python

$
0
0

Dataframes are mainly used in python for the analysis of tabular data. In this article, we will discuss how we can convert a list of dictionaries to a dataframe in python.

List of Dictionaries to Dataframe Using pandas.DataFrame()

The dataframe objects are defined in the pandas module. To create a dataframe from a given list of dictionaries, we can use the DataFrame() method. The DataFrame() method object takes a list of dictionaries as input argument and returns a dataframe created from the dictionaries. Here, the column names for the dataframe consist of the keys in the python dictionary. The values of each dictionary are transformed into the rows of the dataframe. You can create a dataframe from a list of dictionaries using the pandas.DataFrame() method as follows.

import pandas as pd

listOfDict = [{'Name': 'Aditya', 'Roll': 1, 'Language': 'Python'}, {'Name': 'Sam', 'Roll': 2, 'Language': 'Java'},
              {'Name': 'Chris', 'Roll': 3, 'Language': 'C++'}, {'Name': 'Joel', 'Roll': 4, 'Language': 'TypeScript'}]
print("THe list of dictionaries is:")
print(listOfDict)
df = pd.DataFrame(listOfDict)
print("The dataframe is:")
print(df)

Output:

THe list of dictionaries is:
[{'Name': 'Aditya', 'Roll': 1, 'Language': 'Python'}, {'Name': 'Sam', 'Roll': 2, 'Language': 'Java'}, {'Name': 'Chris', 'Roll': 3, 'Language': 'C++'}, {'Name': 'Joel', 'Roll': 4, 'Language': 'TypeScript'}]
The dataframe is:
     Name  Roll    Language
0  Aditya     1      Python
1     Sam     2        Java
2   Chris     3         C++
3    Joel     4  TypeScript

In the dataframe, a column is created for each key in the list of dictionaries. If a dictionary doesn’t have any specific key, the value NaN is given to the column corresponding to the specific key in the row representing the dictionary.

List of Dictionaries to Dataframe in Python Using pandas.dataframe.from_dict() Method

Instead of using the DataFrame() method, we can also use the from_dict() method to convert a list of dictionaries to a dataframe in Python. The from_dict() method takes a list of dictionaries as its input argument and returns a dataframe. Again, the column names for the dataframe consist of the keys in the dictionary. The values of each dictionary are transformed into the rows of the dataframe. You can create a dataframe from a list of dictionaries using the from_dict() method as follows.

import pandas as pd

listOfDict = [{'Name': 'Aditya', 'Roll': 1, 'Language': 'Python'}, {'Name': 'Sam', 'Roll': 2, 'Language': 'Java'},
              {'Name': 'Chris', 'Roll': 3, 'Language': 'C++'}, {'Name': 'Joel', 'Roll': 4, 'Language': 'TypeScript'}]
print("THe list of dictionaries is:")
print(listOfDict)
df = pd.DataFrame.from_dict(listOfDict)
print("The dataframe is:")
print(df)

Output:

THe list of dictionaries is:
[{'Name': 'Aditya', 'Roll': 1, 'Language': 'Python'}, {'Name': 'Sam', 'Roll': 2, 'Language': 'Java'}, {'Name': 'Chris', 'Roll': 3, 'Language': 'C++'}, {'Name': 'Joel', 'Roll': 4, 'Language': 'TypeScript'}]
The dataframe is:
     Name  Roll    Language
0  Aditya     1      Python
1     Sam     2        Java
2   Chris     3         C++
3    Joel     4  TypeScript

You can observe that the dataframe generated by the from_dict() method is similar to the dataframe generated by the DataFrame() method.

List of Dictionaries to Dataframe Using pandas.dataframe.from_records(Data)

To create a dataframe from a list of dictionaries in Python, we can also use the from_records() method. The from_records() method takes a list of dictionaries as its input argument and returns a dataframe. Again, the column names for the dataframe consist of the keys in the dictionary. The values of each dictionary are transformed into the rows of the dataframe. You can create a dataframe from a list of dictionaries using the from_records() method as follows.

import pandas as pd

listOfDict = [{'Name': 'Aditya', 'Roll': 1, 'Language': 'Python'}, {'Name': 'Sam', 'Roll': 2, 'Language': 'Java'},
              {'Name': 'Chris', 'Roll': 3, 'Language': 'C++'}, {'Name': 'Joel', 'Roll': 4, 'Language': 'TypeScript'}]
print("THe list of dictionaries is:")
print(listOfDict)
df = pd.DataFrame.from_records(listOfDict)
print("The dataframe is:")
print(df)

Output:

THe list of dictionaries is:
[{'Name': 'Aditya', 'Roll': 1, 'Language': 'Python'}, {'Name': 'Sam', 'Roll': 2, 'Language': 'Java'}, {'Name': 'Chris', 'Roll': 3, 'Language': 'C++'}, {'Name': 'Joel', 'Roll': 4, 'Language': 'TypeScript'}]
The dataframe is:
     Name  Roll    Language
0  Aditya     1      Python
1     Sam     2        Java
2   Chris     3         C++
3    Joel     4  TypeScript

Conclusion

In this article, we have discussed three ways to convert a list of dictionaries to a dataframe in python. All three methods are semantically similar and produce the same result. To learn more about dictionaries, you can read this article on dictionary comprehension in python. You might also like this article on list comprehension in python.

The post List of Dictionaries to Dataframe in Python appeared first on PythonForBeginners.com.


Viewing all articles
Browse latest Browse all 22462

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>