I am new using pandas. I am asking about how to insert specific columns using to_sql.

This is my explanation.

I have a database with a table datasiswa with columns:

  • id: as int, with autoincrement, as primary key
  • name: string
  • age: string

And I have an excel file with header name and age.

How to use to_sql to insert in fields name and age only, without ids.

enter image description here

  • $\begingroup$ Is hiding your entire path really necessary? $\endgroup$ Apr 11, 2019 at 0:41
  • $\begingroup$ If I understood correctly, you want to fill a SQL Table entries from a Excel file, and this table should create IDs automatically. $\endgroup$ Apr 11, 2019 at 0:42
  • $\begingroup$ @PedroHenriqueMonforte Hide because credential our server, yes i just want insert name and age, whitout ids, because id automatically generate by autoincrement. $\endgroup$ Apr 11, 2019 at 2:27

1 Answer 1


You'll need to perform two steps:

  1. You get ride of the id column in the pandas dataframe:

  2. Add the index=False param: to the .to_sql() method:

    selected.to_sql('dataiswa', engine, index=False)
  • $\begingroup$ 'id' column is not in the dataframe, it's in the database $\endgroup$ Jul 23, 2020 at 22:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.