1

This is probably a question with a somewhat obvious answer, but I can't seem to figure out what's wrong here.

def conv_pd_df():
    spreadsheet = client.open(spreadsheet_id).sheet1
    data = spreadsheet.get_all_values()
    index = str(data[0][0])
    headers = data.pop(0)

    df = pd.DataFrame(data, columns=headers, index=None)
    df = df.set_index(index)

    mask = df.applymap(lambda x: key in str(x))
    df1 = df[mask.any(axis=1)]
    return df1


def highlight(df1):
    for x in df1:
        print(x) # filler


if __name__ == '__main__':
    conv_pd_df()
    highlight(df1) # this is where the error is

I keep getting 'unresolved reference' in my highlight(df1) when trying to execute the code. I tried creating a blank pandas df = df1 at the very beginning to try to combat the issue but it still gives me the same error. I thought I passed the variables right, so what's going on here? this happens even when I don't put if __name__ == '__main__': and just run the functions directly too.

2

2 Answers 2

5

It starts with the code under if __name__ == '__main__': and you don't have a variable df1 defined at that point. The df1 in conv_pd_df() only exists in the scope of that function

You should set df1 in your __main__ equal to the return of conv_pd_df()

df1  = conv_pd_df()
3

Your main is:

if __name__ == '__main__':
    conv_pd_df()
    highlight(df1)

Granted, conv_pd_df() returns your df1 dataframe, but you don't bind that return value, therefore there is no variable named df1. The following will fix that reference issue:

if __name__ == '__main__':
    df1 = conv_pd_df()
    highlight(df1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.