【发布时间】:2022-01-15 21:13:25
【问题描述】:
背景 -
我是初学者,正在编写他们的第一个严肃程序;进行了 API 调用,它返回一个响应,我将其保存为 .json 文件和数据框 (df),然后再进行一些数学和转换并保存为名为 exceptions_df 的新数据框。最后,我希望将df 和exceptions_df 都写入.xlsx 文件(def xlsx_writer())。
问题 -def xlsx_writer(): 将 exception_df 写入 .xlsx 文件没有问题。但是,我也希望它可以将df 写入同一个文件。
虽然我可以使用 df = def ownership_qc() 访问 df 变量,但该函数还调用其他函数 (df = unpack_response()),而后者本身又调用另一个名为 api_response = response_writer()) 的函数。你能看到我的问题吗?通过使用此方法,我的代码将不必要地重复自身(即再次调用相关函数)。我当前的代码可以做到这一点并且“有效”,但是重复代码似乎很愚蠢;而且速度超级慢。
相关功能 - def xlsx_writer - 这是我想在df 中使用的功能。它目前通过df = ownership_qc() 来实现,但是根据上面的描述,它很慢:
def xlsx_writer():
exceptions_df = ownership_exceptions()
# This is where I call df and cause the a chain reaction of ownership_qc() calling function it requires and so forth.
df = ownership_qc()
timestr = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M")
filename = 'ownership_exceptions_'+timestr
writer = pd.ExcelWriter(filename+'.xlsx', engine='xlsxwriter')
exceptions_df.to_excel(writer, sheet_name='Ownership Exceptions Report', startrow=1, header=False, index=False)
df.to_excel(writer, sheet_name='Raw Extract', startrow=1, header=False, index=False)
workbook = writer.book
worksheet = writer.sheets['Ownership Exceptions Report']
(max_row, max_col) = exceptions_df.shape
column_settings = [{'header': column} for column in exceptions_df.columns]
worksheet.add_table(0, 0, max_row, max_col - 1, {'columns': column_settings})
worksheet.set_column(0, max_col - 1, 12)
xlsx_writer()
def ownership_exceptions(): -
这是我在def xlsx_writer() 中调用以访问df 的函数。问题是这个函数调用了另一个函数,并开始了连锁反应:
# This is the function I am calling to acces `df`, however this function calls another function.
def ownership_exceptions():
df = ownership_qc()
df = df[(df['Entity ID %'] != 1.000000) & (df['Account # %'] != 1.000000)]
df.drop(df.index[df['Entity ID %'] == '1'], inplace=True)
exceptions_df = df.drop(['Model Type', 'Valuation (USD)', 'Top Level Legal Entity', 'Financial Service', 'Account Close Date'], axis=1)
return df
我的想法
- 有没有一种方法可以访问
df而无需调用它的函数 (def ownership_exceptions) 并因此调用所有其他相关函数? - 我是否应该简单地创建一个新函数,将 API 响应 (.json) 加载回数据帧并以这种方式访问?
【问题讨论】:
-
更改您的代码,让
ownership_exceptions将df作为参数,这样您就可以将其传入,而不是让它从头开始重新加载所有内容。 -
ownership_exceptions返回df,而不是exceptions_df- 这是故意的吗? -
抱歉 - 错字,应该返回 exceptions_df。
标签: python json pandas function