I have a big dataframe in pandas. I need to put that dataframe by column or columns into sqlite database tables, which i made earlier. Adding fraction of dataframe columns.
VendorID | tpep_pickup_datetime | tpep_dropoff_datetime | passenger_count |
1 | 2020-01-01 00:28:15 | 2020-01-01 00:33:03 | 1.0
1 | 2020-01-01 00:35:39 | 2020-01-01 00:43:04 | 1.0
1 | 2020-01-01 00:47:41 | 2020-01-01 00:53:52 | 1.0
1 | 2020-01-01 00:55:23 | 2020-01-01 01:00:14 | 1.0
2 | 2020-01-01 00:01:58 | 2020-01-01 00:04:16 | 1.0
I found earlier code block in stackoverflow, which can help me adding only one column to database table. The problem is, when iam trying to add few or more columns. Its simply not working. I pretty new to coding. This is code the block, which i found:
crsr = conn.cursor()
sql = "INSERT INTO vendor (VendorID) VALUES (?)"
# extract column and convert to list of single-value tuples
data = [(x,) for x in df['VendorID']]
crsr.executemany(sql, data)
conn.commit()
Is there a simplier or better way to add one or multiple columns from pandas to sqlite database tables ? With for example tpep_pickup_datetime
and tpep_dropoff_datetime
Thanks :)
CodePudding user response:
You do not do it like this at all. You use the Panda's to_sql feature. For a more detailed answer, you, of course, need to provide a proper question.