En måde (blandt mange) at gøre dette på ville være:
for at hente maksimalt Id
og gem det i en variabel (lad os kalde det max_id
):
select max(Id) from quotes;
nu kan vi gøre dette:
Original DF:
In [55]: quote_df
Out[55]:
Adj_Close Close Date High Low Open Symbol Volume
0 170.572764 173.990005 2015-12-31 175.649994 173.970001 175.089996 DIA 5773400
1 172.347213 175.800003 2015-12-30 176.720001 175.619995 176.570007 DIA 2910000
2 173.50403 176.979996 2015-12-29 177.25 176.00 176.190002 DIA 6145700
.. ... ... ... ... ... ... ... ...
213 88.252244 89.480003 2016-01-06 90.099998 89.080002 89.279999 IWN 1570400
214 89.297697 90.540001 2016-01-05 90.620003 89.75 90.410004 IWN 2053100
215 88.893319 90.129997 2016-01-04 90.730003 89.360001 90.550003 IWN 2540600
[1404 rows x 8 columns]
nu kan vi øge indekset med max_id
:
In [56]: max_id = 123456 # <-- you don't need this line...
In [57]: quote_df.index += max_id
og indstil indeks som Id
kolonne:
In [58]: quote_df.reset_index().rename(columns={'index':'Id'})
Out[58]:
Id Adj_Close Close Date High Low Open Symbol Volume
0 123456 170.572764 173.990005 2015-12-31 175.649994 173.970001 175.089996 DIA 5773400
1 123457 172.347213 175.800003 2015-12-30 176.720001 175.619995 176.570007 DIA 2910000
2 123458 173.50403 176.979996 2015-12-29 177.25 176.00 176.190002 DIA 6145700
... ... ... ... ... ... ... ... ... ...
1401 123669 88.252244 89.480003 2016-01-06 90.099998 89.080002 89.279999 IWN 1570400
1402 123670 89.297697 90.540001 2016-01-05 90.620003 89.75 90.410004 IWN 2053100
1403 123671 88.893319 90.129997 2016-01-04 90.730003 89.360001 90.550003 IWN 2540600
[1404 rows x 9 columns]
Nu skulle det være muligt at skrive denne DF til PostgreSQL specificerende (index=False
)