简体   繁体   English

Spark - 从 DataFrame 中提取单个值

[英]Spark - extracting single value from DataFrame

I have a Spark DataFrame query that is guaranteed to return single column with single Int value.我有一个 Spark DataFrame 查询,它保证返回具有单个 Int 值的单列。 What is the best way to extract this value as Int from the resulting DataFrame?从生成的 DataFrame 中将该值提取为 Int 的最佳方法是什么?

You can use head你可以用head

df.head().getInt(0)

or first或者first

df.first().getInt(0)

Check DataFrame scala docs for more details查看DataFrame scala 文档以获取更多详细信息

This could solve your problem.这可以解决您的问题。

df.map{
    row => row.getInt(0)
}.first()

Pyspark ,如果dataframe是具有one column作为响应的单个实体,则您可以简单地获取第一个元素,否则,将返回一整row ,然后您必须获得dimension-wise响应,即df.head()[0][0]

df.head()[0]

If we have the spark dataframe as :如果我们将火花数据框设为:

+----------+
|_c0       |
+----------+
|2021-08-31|
+----------+

x = df.first()[0]
print(x)

2021-08-31

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM