如何使用PySpark从数据框中提取单个(列/行)值? [英] How to extract a single (column/row) value from a dataframe using PySpark?

查看:3341
本文介绍了如何使用PySpark从数据框中提取单个(列/行)值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的火花代码.它工作正常并返回2517.我要做的就是打印"2517度" ...但是我不确定如何将2517度提取到变量中.我只能显示数据框,而不能从中提取值.听起来超级简单,但不幸的是我被卡住了!任何帮助将不胜感激.谢谢!

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)

推荐答案

这是替代方法:

df.first()['column name']

它将为您提供所需的输出.您可以将其存储在变量中.

it will give you the desired output. you can store it in a variable.

这篇关于如何使用PySpark从数据框中提取单个(列/行)值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆