【发布时间】:2023-04-08 18:08:01
【问题描述】:
我是一名金融分析师,拥有大约两个月的 Python 经验,我正在开展一个使用 Python 和 SQL 来自动编译报告的项目。该过程涉及访问保存在共享驱动器中的不断变化数量的 Excel 文件,从每个选项卡(摘要和报价)中拉出两个选项卡,并将数据集组合成两个大型“报价”和“摘要”表。下一步是从每个列中提取不同的列,合并、计算等。
问题是数据集最终是 3.4 毫米行和大约 30 列。我在下面编写的程序可以运行,但完成第一部分(创建数据帧列表)需要 40 分钟,另外需要 4.5 小时来创建数据库和导出数据,更不用说使用大量内存了。
我知道一定有更好的方法来实现这一点,但我没有 CS 背景。任何帮助将不胜感激。
import os
import pandas as pd
from datetime import datetime
import sqlite3
from sqlalchemy import create_engine
from playsound import playsound
reportmonth = '2020-08'
month_folder = r'C:\syncedSharePointFolder'
os.chdir(month_folder)
starttime = datetime.now()
print('Started', starttime)
c = 0
tables = list()
quote_combined = list()
summary_combined = list()
# Step through files in synced Sharepoint directory, select the files with the specific
# name format. For each file, parse the file name and add to 'tables' list, then load
# two specific tabs as pandas dataframes. Add two columns, format column headers, then
# add each dataframe to the list of dataframes.
for xl in os.listdir(month_folder):
if '-Amazon' in xl:
ttime = datetime.now()
table_name = str(xl[11:-5])
tables.append(table_name)
quote_sheet = pd.read_excel(xl, sheet_name='-Amazon-Quote')
summary_sheet = pd.read_excel(xl, sheet_name='-Amazon-Summary')
quote_sheet.insert(0,'reportmonth', reportmonth)
summary_sheet.insert(0,'reportmonth', reportmonth)
quote_sheet.insert(0,'source_file', table_name)
summary_sheet.insert(0,'source_file', table_name)
quote_sheet.columns = quote_sheet.columns.str.strip()
quote_sheet.columns = quote_sheet.columns.str.replace(' ', '_')
summary_sheet.columns = summary_sheet.columns.str.strip()
summary_sheet.columns = summary_sheet.columns.str.replace(' ', '_')
quote_combined.append(quote_sheet)
summary_combined.append(summary_sheet)
c = c + 1
print('Step', c, 'complete: ', datetime.now() - ttime, datetime.now() - starttime)
# Concatenate the list of dataframes to append one to another.
# Totals about 3.4mm rows for August
totalQuotes = pd.concat(quote_combined)
totalSummary = pd.concat(summary_combined)
# Change directory, create Sqlite database, and send the combined dataframes to database
os.chdir(r'H:\AaronS\Databases')
conn = sqlite3.connect('AMZN-Quote-files_' + reportmonth)
cur = conn.cursor()
engine = create_engine('sqlite:///AMZN-Quote-files_' + reportmonth + '.sqlite', echo=False)
sqlite_connection = engine.connect()
sqlite_table = 'totalQuotes'
sqlite_table2 = 'totalSummary'
totalQuotes.to_sql(sqlite_table, sqlite_connection, if_exists = 'replace')
totalSummary.to_sql(sqlite_table2, sqlite_connection, if_exists = 'replace')
print('Finished. It took: ', datetime.now() - starttime)
'''
【问题讨论】:
-
考虑完全避免
pandas并将每个 Excel 电子表格保存为 CSV(您应该已经这样做了!),然后通过 Python 或 sqlite3 CLI 将 CSV 导入 SQLite。 -
我对python一无所知,尽可能避免使用MS-Excel。但是,当您导入 SQLite 时,您可以通过将 SQL 语句封装在事务中来节省大量时间:1)在 SQL 语句的最开头:
BEGIN TRANSACTION;2)在 SQL 语句的最后:COMMIT;
HTH -
@Parfait 你能告诉我为什么我应该已经保存到 CSV 吗?另外,在 CSV 与 pandas 中组织导入数据有什么优势?
标签: python excel database pandas sqlite