How to extract a single (column/row) value from a dataframe using PySpark?

不羁的心 提交于 2020-02-25 22:43:31

问题


Here's my spark code. It works fine and returns 2517. All I want to do is to print "2517 degrees"...but I'm not sure how to extract that 2517 into a variable. I can only display the dataframe but not extract values from it. Sounds super easy but unfortunately I'm stuck! Any help will be appreciated. Thanks!

df = sqlContext.read.format("csv").option("header", "true").option("inferSchema", "true").option("delimiter", "\t").load("dbfs:/databricks-datasets/power-plant/data")

df.createOrReplaceTempView("MyTable")

df = spark.sql("SELECT COUNT (DISTINCT AP) FROM MyTable")

display(df)

回答1:


I think you're looking for collect. Something like this should get you the value:

df.collect()[0]['count(DISTINCT AP)']

assuming the column name is 'count(DISTINCT AP)'



来源:https://stackoverflow.com/questions/55406069/how-to-extract-a-single-column-row-value-from-a-dataframe-using-pyspark

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!