我如何设置数据并索引到熊猫数据框

时间:2020-05-24 11:11:45

标签: python pandas apache-spark-sql

我有这样的查询

test = spark.sql("select mg.moviegenreid, sum(quantity) as total \
                from moviegenre mg \
                join movie m on m.moviegenreid = mg.moviegenreid \
                join detailtransaction dt on dt.movieid = m.movieid \
                join headertransaction ht on ht.transactionid = dt.transactionid \
                group by mg.moviegenreid \
                having sum(quantity) \
                order by total desc \
                limit 5")

然后我将其插入熊猫数据框

data = test.toPandas()

我只想使用

x = data[{"moviegenreid"}]
y = data[{"total"}
val = pd.DataFrame(data=y,index=x)
val.plot.bar()

我总是会收到这样的错误

ValueError: Index data must be 1-dimensional

1 个答案:

答案 0 :(得分:0)

{尝试:

x = data["moviegenreid"]
y = data["total"]
val = pd.DataFrame({'data'=y},index=x)
val.plot.bar()