I have several pandas Dataframe
that I wish to write into a SQL database
. However, because the existing SQL database
might not have that particular column name
that was in the pandas Dataframe
, I get an error message saying that the column in the table was not found, thus unable to append data
.
# Example:
df1
out= column1, column2, column3, column4
value1, value2, value3, value4
df2
out= columnA, columnB, columnC
valueA, valueB, valueC
# Initially I concat the df together and save it into SQL
combined_data = pandas.concat([df1, df2], axis=1,
join='inner')
pandas.DataFrame.to_sql(combined_data, name='table1', con=engine,
if_exists='append', index=False)
However, because this table has already been created, with all the columns, if df2 was to have additional columns, i get an error message.
df2
out= columnA, columnB, columnC, columnD, columnE, columnF
valueA, valueB, valueC, valueD, valueE, valueF
How do i structure a code, that would create new columns in the existing SQL table
, with the names of these columns, as the missing column names from pandas Dataframe
?
I think I can add new columns with the below sql code
connection.execute("ALTER TABLE table1 ADD COLUMN new_column INTEGER DEFAULT 0")
But how do I make sure that the new_column
that was added, follows the column name in df2?