CBSE Class 12 Informatics Practices Question 69 of 70

Importing/Exporting Data between CSV Files/MySQL and Pandas — Question 6

Back to all questions
6
Question

Question 6

Consider the SDF DataFrame storing the sales records of 100 salesmen. Write a program that stores only the first 25 rows of the DataFrame in a table on MySQL database.

Solution
import pandas as pd
from sqlalchemy import create_engine
import pymysql

def sales(sdf):
    engine = create_engine('mysql+pymysql://root:Mypass@localhost/company')
    mycon = engine.connect()
    first_25_rows = sdf.head(25)
    first_25_rows.to_sql('Records', mycon, if_exists = 'replace', index = False)
Answer