[Python] Why am I getting a nameError: name undefined error code when the name in question is...

Discussão em 'Python' iniciado por Stack, Setembro 5, 2025.

  1. Stack

    Stack Membro Participativo

    So I'm doing a practice project from youtube and I made sure to type everything exactly as the guy in the video typed it, except for input lines I made slight changes to that shouldn't affect my main problem. I'm getting a nameError: name undefined message when I try to run the code but the name in question is defined just a few lines before the line I'm trying to call the variable in. It's on line 91. I'll post the code causing the issue below, if any more experienced coders could take a look and figure out what I did wrong that would be greatly appreciated. Note I'm on Windows 11 using pycharm, python version 3.13.

    code--

    def plot_transactions(df):
    df.set_index("date", inplace = True)

    income_df = df[df["category"] == "Income"].resample("D").sum().reindex(df.index, fill_value = 0)

    expense_df = df[df["category"] == "Expense"].resample("D").sum().reindex(df.index, fill_value = 0)



    plt.figure(figsize = (10,5))
    plt.plot(income_df.index, income_df["amount"], label = "Income", color = "g")
    plt.plot(expense_df.index, expense_df["amount"], label = "Expense", color = "r")


    full error message--

    Traceback (most recent call last):
    File "C:\Users\Brien Dalby\PycharmProjects\PythonProject4\main.py", line 91, in <module>
    plt.plot(income_df.index, income_df["amount"], label = "Income", color = "g")
    ^^^^^^^^^
    NameError: name 'income_df' is not defined


    I made sure it was in the correct case and the correct spelling. Google said to make sure I had all the necessary modules imported which I believe I do and it said it could be a scope issue ie trying to call a local scope from a global scope but I've called variables from inside of functions before so I'm not sure what's the problem.

    Continue reading...

Compartilhe esta Página