简体   繁体   English

尝试添加 pandas dataframe 时 sqlite3 表未更新

[英]sqlite3 table is not updating when trying to add pandas dataframe

I am trying to add a Pandas data frame to a sqlite3 table in my Django project.我正在尝试将 Pandas 数据框添加到我的 Django 项目中的 sqlite3 表中。 However, when I try to add it to the table, nothing updates and no errors are printed in the console.但是,当我尝试将其添加到表中时,控制台中不会打印任何更新和错误。 For reference, the beginning of my code is correctly formatting the date in the csv file and then I am attempting to add it to an empty but existing table named 'daos_transaction' with the same attributes 'from_address', 'to_address', 'date_time', and 'amount'.作为参考,我的代码开头正确格式化了 csv 文件中的日期,然后我尝试将其添加到一个名为 'daos_transaction' 的空表中,并具有相同的属性 'from_address'、'to_address'、'date_time' , 和“金额”。

import pandas as pd
import sqlite3

# Reads data from csv file
df = pd.read_csv("correct/path/to/csv/file")

# Formats the date
for dt in range(len(df['date_time'])):
    no_t_string = df['date_time'][dt].replace('T', ' ')
    clean_string = no_t_string.split('+')[0]
    df['date_time'][dt] = clean_string

# Converts the column to a date time field
df['date_time'] = df['date_time'].astype('datetime64[ns]')

cnx = sqlite3.connect('correct/path/to/db')
df.to_sql('daos_transaction', cnx, if_exists='append')

I ended up figuring it out.我最终弄清楚了。 It was because I had to set index=False这是因为我必须设置 index=False

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM