Skip to main contentSkip to solution

Which of the following function will export data from Pandas to MySQL?

Solution

✅ Correct Option: 1

DataFrame.to_sql() writes a DataFrame to a SQL table, so it is the function that exports data from Pandas to MySQL. It takes the table name and a database connection. The if_exists argument sets what happens if the table already exists ("fail", "replace" or "append"), and index decides whether the DataFrame index is written as a column.


Option 2: read_sql_query() works in the opposite direction, importing data from MySQL into a DataFrame.

Options 3 and 4: write_sql_query() and DataFrame.from_sql() do not exist in Pandas.

Memory trick: to_sql sends data out to SQL, while read_sql brings data in.

Keyboard Shortcuts

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