TransWikia.com

Pandas to_sql insert only some columns

Data Science Asked by Paijo S Kom on June 4, 2021

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

One Answer

You'll need to perform two steps:

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

    df=df.drop(columns=['id'])
    
  2. Add the index=False param: to the .to_sql() method:

    selected.to_sql('dataiswa', engine, index=False)
    

Answered by Hernán Borré on June 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP