so Im attempting to updated records in database by id's.
- The problem is it overwrites all rows ```id and Cost`` with the last row of the list
- I dont get any errors, but it should match the id's then replace only
Cost
database:
table =
id Cost
000074800795 157.05974458228403
000074801136 80.637745302714
000074801299 7
000074800300 13
000074800955 10
my code:
df = pd.DataFrame({ "id": [000074800795 , 000074801136, 000074801299,000074800300, 000074800955] ,
"Cost" : [157.05974458228403 ,80.637745302714, 7, 13, 10] })
# replacing Null
df = df.where((pd.notnull(df)), None)
# reset index for For loop
df = df.reset_index()
df.id = df.id.astype(str)
#df.Cost = df.Cost.astype(str)
df.dtypes
out[101]:
index int64
id object
Cost object
dtype: object
values_list = []
for i in range(len(df)):
values_list.append({ "id": df["id"][i] ,
"Cost": df["Cost"][i] ,
})
print(values_list[0:2])
[{'id': '000074800795', 'Cost': 157.05974458228403}, {'id': '000074801136', 'Cost': 80.637745302714}]
Loading at db.
The problem is it overwrite ```id and Cost`` with the last row of the list
I dont get any errors, but it should match the id's then replace only
Cost
engine = db.create_engine("my connection")
connection = engine.connect()
metadata = db.MetaData()
# Creating Table
data = db.Table('table', metadata,
db.Column('id', db.String(100), nullable=True),
db.Column('Cost', db.String(100), nullable=True),
extend_existing=True
)
metadata.create_all(engine)
query = db.update(data)
ResultProxy = connection.execute(query, values_list)
engine.dispose()
Output:
table =
id Cost
000074800955 10
000074800955 10
000074800955 10
000074800955 10
000074800955 10