Pandas NameError:未定义名称“df”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40991725/
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-14 02:34:41  来源:igfitidea点击:

Pandas NameError: name 'df' is not defined

pythonpandasdata-science

提问by Freddy SIEWE

This is my problem: Cousera course on Apllied Data Science in Python I am doing Assigment 2.

这是我的问题:关于 Python 应用数据科学的 Cousera 课程我正在做分配 2。

Question 1 Which country has won the most gold medals in summer games? This function should return a single string value.

问题 1 哪个国家在夏季奥运会上获得的金牌最多?此函数应返回单个字符串值。

This my code:

这是我的代码:

def answer_one():
  return df[df['Gold'] == df['Gold'].max()].index(0)
answer_one()

This is the error which I am getting:

这是我得到的错误:

NameError: name 'df' is not defined

回答by Nelson Dinh

Firstly, maybe your df object is not global variable. If so, you may pass df as an argument to the function.

首先,也许您的 df 对象不是全局变量。如果是这样,您可以将 df 作为参数传递给函数。

For the syntax, your access to the index should be .index[0]rather than .index(0).

对于语法,您对索引的访问应该是.index[0]而不是.index(0)

A more compact solution is return str(df['Gold'].idxmax()).

更紧凑的解决方案是return str(df['Gold'].idxmax()).

回答by Nelson Dinh

Please try to use .Hope so it will work

请尝试使用 .Hope 这样它会起作用

return df['Gold'].idxmax()

return df['Gold'].idxmax()