1

如何将数据框导入 Pandas 数据框并将其转换为字典?

我有从 Spark 创建的这个数据框,

sc = SparkContext(appName="PythonSQL")
    sqlContext = SQLContext(sc)

    path = os.path.join(os.environ['SPARK_HOME'], "examples/src/main/resources/people.json")

# Create the DataFrame
df = sqlContext.read.json(path)

# Register this DataFrame as a table.
df.registerTempTable("people")

# SQL statements can be run by using the sql methods provided by sqlContext
teenagers = sqlContext.sql("SELECT name FROM people")

sc.stop()

当我尝试将其导入熊猫时,

teenagers = pd.DataFrame(teenagers, columns=['name'])

我得到这个错误,

[client 127.0.0.1:50885] PandasError:DataFrame 构造函数未正确调用!

毕竟,我只是想将数据框转换成字典,

dict = teenagers.set_index('name').to_dict()
print dict

有任何想法吗?

4

1 回答 1

4

可以使用该方法将 Spark DataFrame 转换为 pandas DataFrame toPandas

于 2015-08-17T03:28:58.077 回答