Convert dictionary to dataframe Python
In this post, you will learn how to convert Dictionary to Pandas Dataframe in Python programming.
Python has an in-built dictionary called dict, which we can use to create an arbitrary definition of a character string. The dict() is a constructor which is used to create instances of the class dict. Dictionary is a collection whose values are accessible by key.
A Dataframe is a tabular-like data structure containing an ordered collection of columns. Each column can be of different data types, like numeric, boolean, strings, etc. It has both a row and a column index. It is basically used for analytical purposes.
Pandas DataFrame pd.DataFrame.from_dict() class-method is used to convert a dictionary to a pandas dataframe.
pd.DataFrame.from_dict(data, orient='columns', dtype=None)
This method accepts the following parameters:
- data: It is a dict or array like object to create DataFrame.
- orient: The orientation of the data. It allows values 'columns' and 'index'. The default is 'columns'.
- columns: A list of values to use as labels when orient='index'. It raises a ValueError if used with orient='columns'.
- dtype: (Optional) Datatype for the data in the pandas dataframe. The datatype of the data in the dictionary will be infered by default.
Convert Dictionary To Dataframe
In the given example, we are passing the key value as a list to the from_dict() method.
import pandas as pd
data = {'name': ['hardik', 'priska', 'advik', 'somya'],
'age': ['7', '9', '8', '10']}
df= pd.DataFrame.from_dict(data)
print(df)
Output of the above code:
name age
0 hardik 7
1 priska 9
2 advik 8
3 somya 10
Convert Dictionary To Dataframe
By default, the keys of the dict become the DataFrame columns.
import pandas as pd
data = {'Sub': ["Phy","Chm","Bio","Eng"], 'Marks': [89,78,70,82]}
df = pd.DataFrame.from_dict(data)
print(df)
Output of the above code:
Sub Marks
0 Phy 89
1 Chm 78
2 Bio 70
3 Eng 82
Convert Dictionary To Dataframe with index orient
In the given example, we have changed the orientation of the Dataframe from column to index.
import pandas as pd
data = {'Sub': ["Phy","Chm","Bio","Eng"],
'Marks': [89,78,70,82]}
df = pd.DataFrame.from_dict(data,orient='index',
columns=['A','B','C','D'])
print(df)
Output of the above code:
A B C D
Sub Phy Chm Bio Eng
Marks 89 78 70 82
Convert Dictionary To Dataframe with keys as columns
Here, we convert a dictionary to a pandas dataframe with Dictionary keys as columns in the pandas dataframe. For this, we use the orient = 'columns' parameter in the from_dict() method.
import pandas as pd
data = {'Sub': ["Phy","Chm","Bio","Eng"],
'Marks': [89,78,70,82]}
df = pd.DataFrame.from_dict(data,orient = 'columns')
print(df)
Output of the above code:
Sub Marks
0 Phy 89
1 Chm 78
2 Bio 70
3 Eng 82
Related Articles
Sort dictionary by value Python
Python program to sum all the items in a dictionary
Python program to map two lists into a dictionary
Convert dictionary to dataframe Python
Convert list to dictionary Python
Python dict inside list
Pandas string to datetime
Convert Excel to CSV Python Pandas
Add key value pair to dictionary Python
Python Pandas Dataframe to CSV
Python compare two lists
Remove element from list Python
Python iterate list with index
Python program to sum all the numbers in a list
Python print without newline
Python iterate list with index
Python add list to list
Python convert xml to dict
Python dict inside list
Python convert dict to xml
Python raise keyword