1

I am writing a function to pull data from an excel file

output = pd.Dataframe()
def get_help(sheet,head,file):
    tmp_df = pd.read_excel(file,sheet_name='Cover')
    tmp_df = tmp_df.dropna(how='all')
    tmp_df = tmp_df.dropna(axis=1,how='all')
    fnf = tmp_df.iloc[3,0] 
    currency = tmp_df.iloc[5,0]
    q_date = tmp_df.iloc[7,0]
    
    tmp_df = pd.read_excel(file,sheet_name=sheet,header=head)
    tmp_df = tmp_df.loc[tmp_df['banana'] != '--- End of Sheet ---']
    tmp_df['banana'] = tmp_df['banana'].astype(str).str.replace('hi', '')
    tmp_df['FNF'] = fnf
    tmp_df['banana'] = tmp_df['FNF'] + ' ' + tmp_df['banana']
    global output
    output = pd.concat([tmp_df,output])

This code works perfectly until I define it as a function and call the function in a for loop.

I have tried changing dataframe names and using global to define a variable. I am very new to python, does this have something to do with scope and namespaces? Thank you for reading.

To clarify: I am expecting a dataframe as the output, the actual output is a empty dataframe.

If I run the same code that I put inside the function in that order I get the dataframe I was expecting. However, when I use that same code inside the get_help() function the dataframe returns empty.

asahi
  • 11
  • 3
  • What does not work when you put it in a function? Do you get errors? – Niek de Klein Feb 13 '23 at 15:14
  • Please be specif9ic about your particular problem. You have already defined a function entitled get_help. I don't see where you are calling this function? What doesn't work, what error are you receiving? What is it you are trying to accomplish? – itprorh66 Feb 13 '23 at 15:16
  • 2
    rather than maintain the `global output`, you probably want to `return pd.concat([tmp_df,output])` – JonSG Feb 13 '23 at 15:21
  • @NiekdeKlein I get no error just an empty dataframe at the end – asahi Feb 13 '23 at 16:35

0 Answers0