Skip to content

Problem writing to SQL db with DataFrame column names as numbers #8087

Closed
@klonuo

Description

@klonuo

Example:

import numpy as np
import pandas as pd
from sqlalchemy import create_engine

engine = create_engine('mssql+pyodbc://localhost\\sqlexpress/test')

x1 = pd.DataFrame(np.random.rand(5,3))
x2 = pd.DataFrame(np.random.rand(5,3), columns=['0', '1', '2'])

x1.to_sql('x1', con=engine)
x2.to_sql('x2', con=engine)

which creates these two tables with same column names (numbers as string) but different contents:

x1:

index   0   1   2
0   NULL    NULL    NULL
1   NULL    NULL    NULL
2   NULL    NULL    NULL
3   NULL    NULL    NULL
4   NULL    NULL    NULL

x2:

index   0   1   2
0   0.749669142411769   0.120300003868206   0.424437055115581
1   0.205079144551286   0.977461912401449   0.480845560482874
2   0.635489979788438   0.0149051603743131  0.469838520671857
3   0.765853984144486   0.927480038225488   0.665683952437241
4   0.0688135748993546  0.990064095395479   0.921328636417753

So x1 table and x2 table have same column names, and as expected I can change the values in x1 with sql query, but pandas seems to failed in instructing sqlalchemy to write the data in x1 columns.

Metadata

Metadata

Assignees

No one assigned

    Labels

    IO SQLto_sql, read_sql, read_sql_query

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions