. Advertisement .
..3..
. Advertisement .
..4..
Python dictionary is one of the most versatile data structures. It can store any type of data. Pandas dataframe is a row and column-oriented data structure that is ideal for data analysis and data visualization tasks. This blog is about how you can convert dictionary to pandas dataframe in python.
The way to convert dictionary to Pandas DataFrame in Python
Option 1: Utilize pandas.dataframe().from_dict()
You can utilize pandas.dataframe().from_dict()
to convert a Python Dictionary to a Pandas DataFram. Let’s take a look at an example of this:
import pandas as pd
print(pd.DataFrame.from_dict({
'heeya': 43,
'meet': 45,
'jaydeep': 47,
'romil': 41,
'vrunda': 48,
'nirali': 43}, orient='index').rename(columns={0:'marks'}))
Output
marks heeya 43
meet 45
jaydeep 47
romil 41
vrunda 48
nirali 43
Option 2: Utilize pd.dataframe()
Another way to convert dictionary to Pandas Dataframe in Python is using pd.dataframe().
import pandas as pd
marks_dict = {
'heeya': 43,
'meet': 45,
'jaydeep': 47,
'romil': 41,
'vrunda': 48,
'nirali': 43}
print(pd.DataFrame(list(marks_dict.items()),
columns=['student', 'marks']))
Output
student marks 0
heeya 43 1
meet 45 2
jaydeep 47 3
romil 41 4
vrunda 48 5
nirali 43
Option 3: Use list of items
Utilizing the list(my_dict.items())
to creat list of Dictionary items is a great solution to convert dictionary to Pandas Dataframe in Python. The columns
parameter also allows you to pass the values for the column headers.
For example, the dictionary’s key values do not consist of a list of values. The dictionary keys will be produced as rows in the pandas dataframe by this method.
import pandas as pd
my_dict = {"S.No.": 1, "Item":"CPU", "Quantity": 3, "Price": 5000}
df = pd.DataFrame(list(my_dict.items()), columns = ['Name','Value'])
df
Output
Name | Value | |
0 | S.No. | 1 |
1 | Item | CPU |
2 | Quantity | 3 |
3 | Price | 5000 |
Conclusion
We hope you found our blog post on how to convert dictionary to pandas dataframe in python helpful. If you have any further queries or concerns about this topic, please leave a comment. Thank you for reading; we are always glad when one of our articles provides useful knowledge on this subject!
Read more
Leave a comment