Which of the following function will export data from Pandas to MySQL?
Which of the following function will export data from Pandas to MySQL?
Solution
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.
Related questions:
2025: 27 May Shift 2
2025: 13 May Shift 2
2026: 29 May Shift 2
2026: 29 May Shift 1