Pandas to_sql set column type
First of all, your numbers should be in decimal point format, so we need to replace your decimals with comma.
Next you should ensure that to_sql
function will use float, and You can achieve this with dtype
argument that enable to set a column type (based on sqlalchemy types) when inserting in database. Here the code:
import pandas as pd
from sqlalchemy import create_engine
from sqlalchemy.types import Float # note this import to use sqlalchemy Float type
engine = create_engine('postgresql://{}:{}@{}:5432/{}'.format(USER, DB_PW, HOST, DB))
df = pd.DataFrame({'String2Number': ['0,2', '', '0,0000001']})
# Replacing ',' to '.'
df['String2Number'] = df['String2Number'].apply(lambda x: str(x).replace(',', '.'))
# Set column type as SQLAlchemy Float
df.to_sql(
name='TABLE_NAME',
con=engine,
index=False,
dtype={'String2Number': Float()}
)