我创建了几个全局变量,如下面的代码所示。但是,当我尝试在单个功能中使用它们时,我仍然会遇到相同的错误。
请在下面找到我的代码
def create_df():
global sheet_name, sheet_df
for s in sheets:
sheet_name = s
sheet_df = pd.read_excel(xls, sheet_name=s)
sheet_df = sheet_df[sheet_df.columns.difference(sheet_df.filter(like='Derived').columns,sort=False)]
print("Sheet " + str(s) + " is created as a dataframe successfully")
transform_stage_1_df()
def transform_stage_1_df():
global sheet_df
sheet_df = pd.melt(sheet_df, id_vars='subject_ID', var_name='headers', value_name='dates')
sheet_df['header_extracted'] = [x.split("Date")[0] for x in sheet_df['headers']]
sheet_df['day'] = sheet_df['header_extracted'].str.extract('(d+)', expand=True).astype(int)
sheet_df = sheet_df[sheet_df.groupby(['subject_ID','header_extracted'])['dates'].transform('count').ne(0)].copy()
sheet_df = sheet_df.sort_values(by=['subject_ID','day'])
sheet_df.drop(['header_extracted', 'day'], axis=1, inplace=True)
print("Stage 1 transformation is complete")
if __name__ == '__main__':
print("Execution Started")
print("File read successfully")
sheets = xls.sheet_names
sheet_name = sheet_df = Non_Cholesterol = None
dataFramesDict = dict()
create_df()
add_units()
Non_Cholesterol.to_csv('Output.csv')
基于SO帖子,我已经更新了全局关键字,但我仍然得到UnboundLocalError: local variable 'sheet_df' referenced before assignment
UnboundLocalError Traceback (most recent call last)
<ipython-input-210-dc2f8412235d> in <module>
7 sheet_df = None
8 dataFramesDict = dict()
----> 9 create_df()
10 Non_Cholesterol.to_csv('C:\Users' +
str('Non_cholesterol.csv'),index=None)
11 print("Export successful")
<ipython-input-205-c93604f0da4f> in create_df()
5 sheet_df =sheet_df[sheet_df.columns.difference(sheet_df.filter(like='Derived').columns,sort=False)]
6 print("Sheet " + str(s) + " is created as a dataframe
successfully")
----> 7 transform_stage_1_df()
<ipython-input-206-b59c70018a9b> in transform_stage_1_df()
1 def transform_stage_1_df():
----> 2 sheet_df = pd.melt(sheet_df, id_vars='subject_ID',
var_name='headers', value_name='dates')
3 sheet_df['header_extracted'] = [x.split("Date")[0] for x in
sheet_df['headers']]
4 sheet_df['day'] = sheet_df['header_extracted'].str.extract('(d+)',
expand=True).astype(int)
5 sheet_df =
sheet_df[sheet_df.groupby(['subject_ID','header_extracted'])
['dates'].transform('count').ne(0)].copy()
UnboundLocalError: local variable 'sheet_df' referenced before assignment
您需要初始化脚本正文的变量。当您说global variable_name
时,这意味着您将访问函数之外的变量,而不是名为variable_name
的本地变量。
# Initialize the variables first
sheet_name = None
sheet_df = None
def create_df():
global sheet_name, sheet_df
for s in sheets:
sheet_name = s
sheet_df = pd.read_excel(xls, sheet_name=s)
sheet_df = sheet_df[sheet_df.columns.difference(sheet_df.filter(like='Derived').columns,sort=False)]
print("Sheet " + str(s) + " is created as a dataframe successfully")
transform_stage_1_df()
def transform_stage_1_df():
global sheet_df
sheet_df = pd.melt(sheet_df, id_vars='subject_ID', var_name='headers', value_name='dates')
sheet_df['header_extracted'] = [x.split("Date")[0] for x in sheet_df['headers']]
sheet_df['day'] = sheet_df['header_extracted'].str.extract('(d+)', expand=True).astype(int)
sheet_df = sheet_df[sheet_df.groupby(['subject_ID','header_extracted'])['dates'].transform('count').ne(0)].copy()
sheet_df = sheet_df.sort_values(by=['subject_ID','day'])
sheet_df.drop(['header_extracted', 'day'], axis=1, inplace=True)
print("Stage 1 transformation is complete")
if __name__ == '__main__':
print("Execution Started")
xls = pd.ExcelFile('C:\Users\All.xlsx')
print("File read successfully")
sheets = xls.sheet_names
dataFramesDict = dict()
create_df()