全局变量和赋值前的引用

时间:2018-09-18 14:56:17

标签: python python-3.x pandas runtime-error global-variables

我正在尝试创建一个空的全局熊猫数据框,然后将其附加到数据处理功能中。我在找出问题所在时遇到了麻烦。这是一个最小的示例:

from datetime import datetime
import pandas as pd

def do_stuff_and_append_df():
    dt = datetime.now()
    row = dict.fromkeys(df.columns.tolist())
    row['datetime'] = dt
    row['pduType'] = 'blah'
    df = df.append(row, ignore_index=True)
    print(df.head())

def main():
    # empty dataframe for appending
    header = ['datetime', 'pduType']
    global df 
    df = pd.DataFrame(columns=header)
    # Main loop
    try:
        while True:
            do_stuff_and_append_df()

    except KeyboardInterrupt:
        print('\nSaving and exiting.')
        df.to_csv('data.csv')

main()

我收到错误UnboundLocalError: local variable 'df' referenced before assignment。有人知道为什么我不能在此函数中调用df吗?

4 个答案:

答案 0 :(得分:2)

您还必须在函数do_stuff_and_append_df()下声明“ global df”。

因为函数do_stuff_and_append_df()不知道您是引用本地变量还是全局变量。

答案 1 :(得分:1)

尝试在do_stuff_and_append_df中添加全局声明

def do_stuff_and_append_df():
    global df

答案 2 :(得分:1)

更好的是,不要使用全局变量。将所需的数据帧作为参数传递。

from datetime import datetime
import pandas as pd

def do_stuff_and_append_df(df):
    dt = datetime.now()
    row = dict.fromkeys(df.columns.tolist())
    row['datetime'] = dt
    row['pduType'] = 'blah'
    df = df.append(row, ignore_index=True)
    print(df.head())
    return df

def main():
    # empty dataframe for appending
    header = ['datetime', 'pduType']
    df = pd.DataFrame(columns=header)
    # Main loop
    try:
        while True:
            df = do_stuff_and_append_df(df)

    except KeyboardInterrupt:
        print('\nSaving and exiting.')
        df.to_csv('data.csv')

main()

答案 3 :(得分:0)

如果要使用全局变量,请将其显式放置在全局范围内:

from datetime import datetime
import pandas as pd

def do_stuff_and_append_df():
    dt = datetime.now()
    row = dict.fromkeys(df.columns.tolist())
    row['datetime'] = dt
    row['pduType'] = 'blah'
    df = df.append(row, ignore_index=True)
    print(df.head())

def main():
    # Main loop
    try:
        while True:
            do_stuff_and_append_df()

    except KeyboardInterrupt:
        print('\nSaving and exiting.')
        df.to_csv('data.csv')

if __name__ == "__main__":
    header = ['datetime', 'pduType']
    df = pd.DataFrame(columns=header)

要么,要么查看chepner的答案。最好您不要使用全局变量