我想写一个简单的函数,输出一行数据帧。如果数据帧已经加载到内存中,我不想重新加载它。如果在第一次调用函数时它还没有加载到内存中,我想加载它。你知道吗
我使用Try语句来测试数据帧是否存在(Testing if a pandas DataFrame exists)。由于DF尚未初始化为None或empty,执行此操作的可选选项会产生namererror。你知道吗
global dfIndices
def getSeriesInfo(seriesCode,verbose=False):
try:
if verbose:
print(dfIndices.loc[seriesCode])
else:
print(dfIndices.loc[seriesCode]['Data Item Description'])
# catch when it hasn't even been defined
except NameError:
print('I am here.')
dfIndices = pd.read_excel(dirAppData+'Indicies.xlsx')
dfIndices.set_index('Series ID',inplace=True)
getSeriesInfo(seriesCode,verbose)
getSeriesInfo('A2304402X')
假设我还没有加载dfIndicies dataframe,我希望在第一次调用函数时,Try语句会失败,dataframe会被加载,函数会再次调用,Try会通过,函数会停止执行。你知道吗
相反,我得到了一个无限循环。你知道吗
为了让我能学到一些东西,为什么这不能像预期的那样工作,我应该做些什么不同的事情?你知道吗
谢谢
目前没有回答
相关问题 更多 >
编程相关推荐