Skip to content

create_table(tname: str, df: pd.DataFrame, if_exists='fail')

Marcel Ferrari edited this page Feb 9, 2023 · 1 revision

Creates a new table in the database with the contents of a Pandas DataFrame.

Function

create_table(tname: str, df: pd.DataFrame, if_exists='fail') -> None
  • tname: name of the new table.
  • df: Pandas DataFrame containing the data to be inserted into the new table.
  • if_exists: What to do if the table already exists. Possible values are fail, replace, and append. fail raises an error, replace deletes the existing table and creates a new one, and append inserts the data into the existing table.

Example

# Connect to the database
db = PandaSQLiteDB("example.sql")

# Create a Pandas DataFrame
df = pd.DataFrame({
    'column1': [1, 2, 3],
    'column2': ['A', 'B', 'C']
})

# Create a new table named "my_table" in the database
db.create_table('my_table', df)