Skip to main contentSkip to solution

Which of the following is used to export a DataFrame to MYSQL?

Note: Here 'df' is a DataFrame.

Solution

✅ Correct Option: 1

df.to_sql() writes the rows of a DataFrame into a table in a SQL database such as MySQL. It needs a connection or SQLAlchemy engine, for example df.to_sql('students', con=engine, if_exists='replace', index=False).


Options 2, 3 and 4: read_sql_query(), read_sql() and read_sql_table() all do the opposite. They import data from a database into a DataFrame.

Memory trick: a method called on df (df.to_...) sends data out, while the pd.read_... functions bring data in.

Keyboard Shortcuts

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