Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PySpark中的数据帧未显示_Python_Pyspark_Databricks - Fatal编程技术网

Python PySpark中的数据帧未显示

Python PySpark中的数据帧未显示,python,pyspark,databricks,Python,Pyspark,Databricks,我试图显示一个数据帧,但不知何故它一直告诉我没有定义df!这怎么可能?代码如下: for key, val in mapping_dict.items(): target_table = key files, query, schema = val for file in files: try: df = sqlContext.read.format('csv').options(header='true', charset='UTF-16')

我试图显示一个数据帧,但不知何故它一直告诉我没有定义df!这怎么可能?代码如下:

for key, val in mapping_dict.items():
    target_table = key
    files, query, schema = val
    for file in files:
      try:
        df = sqlContext.read.format('csv').options(header='true', charset='UTF-16').schema(schema).load(file)
        #Convert column names to lowercases and replace spaces with underscores.
        df = df.toDF(*[(c.lower()).replace(' ','_') for c in df.columns])
        #Convert strings to date type.
        df = df.withColumn("date", to_date(df['date']))
        df.registerTempTable("dataTable")
        df = sqlContext.sql(query)
        )
      except Exception as e:
        print(e)
  return print("The loading is completed!")

df.head()

错误是NameError:未定义名称“df”

这是范围界定问题-您应该了解有关代码开发的最佳实践,或者请他人帮助您构建代码

快速而肮脏的解决方案(如果这是一个一次性脚本)是将
global df
放在函数顶部

def your_function(...):
    global df

    for key, val in mapping_dict.items():
        target_table = key
        files, query, schema = val
        for file in files:
    ...

df.head()

您显示的代码不完整-在没有相应的
def
的情况下,
return
在那里做什么?。请提供一份报告。