
DataFrame.to_dict(orient='dict', into=<class 'dict'>)
orientintodf=pd.DataFrame(data={'id': [1, 2, 3],
'name': ['John Deo', 'Max Ruin', 'Arnold'],
'class': ['Four', 'Three', 'Three'],
'mark': [75, 85, 55],
'gender': ['female', 'male', 'male']})
Using this DataFrame we can create the Dictionary by using to_dict().
my_dict=df.to_dict()
print(my_dict)
Output is here
{'id': {0: 1, 1: 2, 2: 3},
'name': {0: 'John Deo', 1: 'Max Ruin', 2: 'Arnold'},
'class': {0: 'Four', 1: 'Three', 2: 'Three'},
'mark': {0: 75, 1: 85, 2: 55},
'gender': {0: 'female', 1: 'male', 2: 'male'}}
my_dict=df.to_dict(orient='dict')
{'id': {0: 1, 1: 2, 2: 3},
'name': {0: 'John Deo', 1: 'Max Ruin', 2: 'Arnold'},
'class': {0: 'Four', 1: 'Three', 2: 'Three'},
'mark': {0: 75, 1: 85, 2: 55},
'gender': {0: 'female', 1: 'male', 2: 'male'}}
my_dict=df.to_dict( orient='list')
Output is here
{'id': [1, 2, 3],
'name': ['John Deo', 'Max Ruin', 'Arnold'],
'class': ['Four', 'Three', 'Three'],
'mark': [75, 85, 55],
'gender': ['female', 'male', 'male']}
my_dict=df.to_dict( orient='series')
Output is here
{'id': 0 1
1 2
2 3
Name: id, dtype: int64, 'name': 0 John Deo
1 Max Ruin
2 Arnold
Name: name, dtype: object, 'class': 0 Four
1 Three
2 Three
Name: class, dtype: object, 'mark': 0 75
1 85
2 55
Name: mark, dtype: int64, 'gender': 0 female
1 male
2 male
Name: gender, dtype: object}
my_dict=df.to_dict( orient='split')
Output is here
{'index': [0, 1, 2],
'columns': ['id', 'name', 'class', 'mark', 'gender'],
'data': [[1, 'John Deo', 'Four', 75, 'female'],
[2, 'Max Ruin', 'Three', 85, 'male'],
[3, 'Arnold', 'Three', 55, 'male']]}
my_dict=df.to_dict( orient='records')
Output is here
[{'id': 1,
'name': 'John Deo',
'class': 'Four',
'mark': 75,
'gender': 'female'},
{'id': 2, 'name': 'Max Ruin', 'class': 'Three', 'mark': 85, 'gender': 'male'},
{'id': 3, 'name': 'Arnold', 'class': 'Three', 'mark': 55, 'gender': 'male'}]
my_dict=df.to_dict( orient='index')
Output is here
{0: {'id': 1,
'name': 'John Deo',
'class': 'Four',
'mark': 75,
'gender': 'female'},
1: {'id': 2,
'name': 'Max Ruin',
'class': 'Three',
'mark': 85,
'gender': 'male'},
2: {'id': 3,
'name': 'Arnold',
'class': 'Three',
'mark': 55,
'gender': 'male'}}
import pandas as pd
from sqlalchemy import create_engine
my_conn = create_engine("mysql+mysqldb://userid:pw@localhost/my_db")
sql="SELECT * FROM student LIMIT 0, 3"
df=pd.read_sql(sql,my_conn)
my_dict=df.to_dict()
my_dict
Output is here
{'id': {0: 1, 1: 2, 2: 3},
'name': {0: 'John Deo', 1: 'Max Ruin', 2: 'Arnold'},
'class': {0: 'Four', 1: 'Three', 2: 'Three'},
'mark': {0: 75, 1: 85, 2: 55},
'gender': {0: 'female', 1: 'male', 2: 'male'}}
df=pd.read_excel("D:\\my_data\\student.xlsx") # Path of the file.
df.to_dict()
We can read one csv file by using read_csv()
df=pd.read_csv("D:\\my_data\\student.csv") # change the path
df.to_dict()
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.