Skip to content Skip to sidebar Skip to footer

Error Despite Global Keyword Being Used To Access Variable Inside Function

I have created few global variables as shown in my code below. However when I try to use them inside the individual functions, I still get the same error. Please find my code below

Solution 1:

You need to initialize the variables at the body of your script. When you say global variable_name it means that you will be accessing the variable outside of the function instead of a local variable named 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()


Post a Comment for "Error Despite Global Keyword Being Used To Access Variable Inside Function"