0

I've to import csv data in sql using sqlAlchemy. The csv has to columns (x, y) but I need to add a third column (delta_y) in the sql database to store processed data.

Using the following code it reads the csv to the sql database but is not creating the actual empty column in the database. Is there a smooth way to inherit was is mapped out in the class?

from sqlalchemy import Column, Integer, Float, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine, update

engine = create_engine('sqlite:///hausarbeit_db.sqlite3', echo=True)
Base = declarative_base()

class Test(Base):
    __tablename__ = "test"
    id = Column(Integer, primary_key=True)
    x = Column(Float)
    y = Column(Float)
    delta_y = Column(Float)


Base.metadata.create_all(engine)

file_name = 'Beispiel-Datensaetze//test.csv'
df = pd.read_csv(file_name)
df.to_sql('test', con=engine, index_label="id", if_exists='replace')
TEST = Base.metadata.tables['test']

I'm also happy to hear any other hints or tips around the code above.

Thanks!

1 Answer 1

1

Can't you add a new empty column in the data-frame after reading from the csv

df["delta_y"] = np.nan 

# or 

df["delta_y"] = ""
Sign up to request clarification or add additional context in comments.

3 Comments

To simple :-D Yes, it's working! However it would be great if someone knows a solution using the already defined class.
I"m not sure whether I followed your above comment properly.. so this does not create an additional column in your table? or you're looking for some other solution?
It's actually doing what it is supposed to do, thanks! I'm just curious if there is another solution using the columns already lined out in the class definition.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.