pandas 熊猫全球数据框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25193715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 22:20:48  来源:igfitidea点击:

pandas global data frame

pythonpandasglobal-variables

提问by Donbeo

Why is this code wrong?

为什么这段代码是错误的?

Isn't D a global variable?

D 不是全局变量吗?

import pandas as pd

D = pd.DataFrame()
D['z'] = [2]


def funz2(z):
    d = pd.DataFrame()
    d['z'] = z
    D = D.append(d)
    print(D)


print(funz2(4))

This is the error message

这是错误信息

In [22]: ---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
<ipython-input-22-68bb930462f5> in <module>()
----> 1 __pyfile = open('''/tmp/py3865JSV''');exec(compile(__pyfile.read(), '''/home/donbeo/Desktop/prova.py''', 'exec'));__pyfile.close()

/home/donbeo/Desktop/prova.py in <module>()
     14 
     15 
---> 16 print(funz2(4))

/home/donbeo/Desktop/prova.py in funz2(z)
     10     d = pd.DataFrame()
     11     d['z'] = z
---> 12     D = D.append(d)
     13     print(D)
     14 

UnboundLocalError: local variable 'D' referenced before assignment

EDIT: If variables are not automatically global. Why does it work?

编辑:如果变量不是自动全局的。为什么有效?

x = 3 

def funz(z):
    return z * x

print(funz(4))

回答by Jeremy Allen

Your funz2can certainly access the Dvariable that you declared outside of it.

funz2当然可以访问D您在外部声明的变量。

The problem you see is because you have declared another Dvariable local to the funzfunction with the line that starts with D=. This local one takes precedence over the other globalish one and thus you get the exception.

您所看到的问题是,因为你已经声明另一个D局部变量的funz函数的行开始用D=。这个本地的优先于另一个全局的,因此你会得到例外。

What you can do is as Alex Woolford suggests and declare the Din the funz function as global using the global statement in effect saying 'see that D there, I don't wanna declare no local D var there I want it to reference that other global one'.

你可以做的是,正如亚历克斯伍尔福德所建议的那样,并D使用 global 语句将 funz 函数中的函数声明为全局函数,实际上是说'看到那里的 D,我不想在那里声明没有本地 D 变量,我希望它引用其他全局变量一'。

回答by Alex Woolford

By default, Python variables are not global in scope. This is by design: it would be dangerous if a function could alter variables defined outside the function. Here's a more eloquent explanation: Using global variables in a function other than the one that created them

默认情况下,Python 变量在范围内不是全局的。这是设计使然:如果函数可以更改在函数外部定义的变量,那将是危险的。这是一个更有说服力的解释:在函数中使用全局变量而不是创建它们的函数

If you want to append rows to D within your function, you could declare it as global:

如果要在函数中将行附加到 D,可以将其声明为全局:

global D = pd.DataFrame()

When reading a variable, Python looks in its local scope first and, if it can't find the name there, it'll start to look in the containing scopes.

在读取变量时,Python 首先在其本地范围内查找,如果在那里找不到名称,它将开始在包含范围内查找。