So, I have a csv file containing data like this:
id type sum_cost date_time
--------------------------------------------------
a1 pound 500 2019-04-21T10:50:06
b1 euro 100 2019-04-21T10:40:00
c1 pound 650 2019-04-21T11:00:00
d1 usd 410 2019-04-21T00:30:00
What I want to do is to insert these data into a database table where the schema is not the same as the csv such that the column in table have like this:
_id , start_time, end_time, pound_cost, euro_cost, count
where I insert from csv to this table such that, id = id, start_time is date_time - 1 hour, end_time is date_time - 30 minutes. For pound_cost and euro_cost, if type is pound insert the value from its sum_cost into pound_cost and add 0 to euro_cost. The same way to euro. and add 1 to the count column.
So, the result of the table will be like this:
_id start_time end_time pound_cost euro_cost count
-----------------------------------------------------------------------------
a1 2019-04-21T09:50:06 2019-04-21T10:20:06 500 0 1
b1 2019-04-21T09:40:06 2019-04-21T10:10:00 0 100 1
c1 2019-04-21T10:00:00 2019-04-21T10:30:00 650 0 1
d1 2019-04-20T23:30:00 2019-04-21T00:00:00 0 410 1
So, how should I insert data to table respect to how I transform values from csv to the table. This is my first time using postgresql and I did not use sql that much so I wonder if there is a function that can do this. Or if not, how can I use Python to transform data and insert them to the table.
Thank you.
appendargument that you can pass which might be useful. More documentation here:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_sql.htmlINSERT into yourmaintable select col1_expression,co12_expression .. from temp_table. Herecol1_expression,col2_expressionare the transformations from the columns of temp table into the the main table. For eg:date_time - interval '1 hour' as end_time