Skip to main contentSkip to solution

Arrange the following steps to import Data from MySQL to Pandas DataFrame 'df':

(A) engine=create_engine('mysql+pymysql:// root:smsmb@localhost:3306/CARSHOWROOM')

(B) import pandas as pd; import pymysql as py; import sqlalchemy

(C) df = pd.read_sql_query('SELECT * FROM INVENTORY', engine)

(D) print(df)

Solution

✅ Correct Option: 2

Imports come first (B), since everything else depends on them. Next the engine is created (A) with the connection string, then the query is run through that engine to fill the DataFrame (C), and finally the DataFrame is printed (D).

(B) Import pandas, pymysql and sqlalchemy

(A) Create the engine with create_engine()

(C) Read the table into df with pd.read_sql_query()

(D) Print df


Options 1, 3 and 4: each puts print(df) or the read_sql_query call before the engine or imports exist, which would raise an error because df or engine would be undefined.

Keyboard Shortcuts

  • Left arrow: Previous question
  • Right arrow: Next question
  • S key: Jump to solution
  • Q key: Jump to question