在pyspark中查找列表的最大值/最小值 [英] Fidning max/min value of a list in pyspark

查看:69
本文介绍了在pyspark中查找列表的最大值/最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个非常微不足道的问题,我很惊讶我在互联网上找不到答案,但是可以在 pyspark 中找到列表的最大值或最小值吗?在 Python 中,它很容易通过

I know this is a very trivial question, and I am quite surprised I could not find an answer on the internet, but can one find the max or min value o a list in pyspark? In Python it is easily done by

max(list)

但是,当我在 pyspark 中尝试相同的操作时,出现以下错误:

However, when I try the same in pyspark I get the following error:

An error was encountered:
An error occurred while calling z:org.apache.spark.sql.functions.max. Trace:
py4j.Py4JException: Method max([class java.util.ArrayList]) does not exist
    at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:318)
    at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:339)
    at py4j.Gateway.invoke(Gateway.java:276)
    at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
    at py4j.commands.CallCommand.execute(CallCommand.java:79)
    at py4j.GatewayConnection.run(GatewayConnection.java:238)
    at java.lang.Thread.run(Thread.java:748)

对我做错了什么有任何想法吗?

Any ideas as to what I am doing wrong?

更新:添加我所做的:这是我的清单:

cur_datelist

输出:

['2020-06-10', '2020-06-11', '2020-06-12', '2020-06-13', '2020-06-14', '2020-06-15', '2020-06-16', '2020-06-17', '2020-06-18', '2020-06-19', '2020-06-20', '2020-06-21', '2020-06-22', '2020-06-23', '2020-06-24', '2020-06-25', '2020-06-26', '2020-06-27', '2020-06-28', '2020-06-29', '2020-06-30', '2020-07-01', '2020-07-02', '2020-07-03', '2020-07-04', '2020-07-05', '2020-07-06', '2020-07-07', '2020-07-08', '2020-07-09', '2020-07-10', '2020-07-11', '2020-07-12', '2020-07-13', '2020-07-14', '2020-07-15', '2020-07-16', '2020-07-17', '2020-07-18', '2020-07-19', '2020-07-20', '2020-07-21', '2020-07-22', '2020-07-23', '2020-07-24', '2020-07-25', '2020-07-26', '2020-07-27', '2020-07-28', '2020-07-29', '2020-07-30', '2020-07-31', '2020-08-01', '2020-08-02', '2020-08-03', '2020-08-04', '2020-08-05', '2020-08-06', '2020-08-07', '2020-08-08', '2020-08-09', '2020-08-10', '2020-08-11', '2020-08-12', '2020-08-13', '2020-08-14', '2020-08-15', '2020-08-16', '2020-08-17', '2020-08-18', '2020-08-19', '2020-08-20', '2020-08-21', '2020-08-22', '2020-08-23', '2020-08-24', '2020-08-25', '2020-08-26', '2020-08-27', '2020-08-28', '2020-08-29', '2020-08-30', '2020-08-31']

类是列表":type(cur_datelist)

<类'列表'>

我认为这是一个普通的pythonic列表.所以当我尝试 max(cur_datelist) 时,我得到了上面提到的错误.

I assumed that to be a normal pythonic list. So when I tried max(cur_datelist), I get the above mentioned error.

推荐答案

pyspark 和 python 的列表没有区别,只是列有区别.这是我的pyspark的结果.

It is not different between pyspark and python for the list but the column is difference. This is the result of my pyspark.

# just a list
l = [1, 2, 3]
print(max(l))

# 3

# dataframe with the array column
df = spark.createDataFrame([(1, [1, 2, 3]), (2, [4, 5, 6])]).toDF('id', 'list')

import pyspark.sql.functions as f

df.withColumn('max', f.array_max(f.col('list'))).show()

#+---+---------+---+
#| id|     list|max|
#+---+---------+---+
#|  1|[1, 2, 3]|  3|
#|  2|[4, 5, 6]|  6|
#+---+---------+---+

您的错误来自python本机和spark列函数之间的max函数重叠!为避免这种情况,请指定您的 pyspark 函数.然后max表示python原件.

Your error comes from the max function overlap between the python native one and the spark column function! To avoid this, specify your pyspark function. Then max denotes the python original.

import pyspark.sql.functions as f

l = ['2020-06-10', '2020-06-11', '2020-06-12', '2020-06-13', '2020-06-14', '2020-06-15', '2020-06-16', '2020-06-17', '2020-06-18', '2020-06-19', '2020-06-20', '2020-06-21', '2020-06-22', '2020-06-23', '2020-06-24', '2020-06-25', '2020-06-26', '2020-06-27', '2020-06-28', '2020-06-29', '2020-06-30', '2020-07-01', '2020-07-02', '2020-07-03', '2020-07-04', '2020-07-05', '2020-07-06', '2020-07-07', '2020-07-08', '2020-07-09', '2020-07-10', '2020-07-11', '2020-07-12', '2020-07-13', '2020-07-14', '2020-07-15', '2020-07-16', '2020-07-17', '2020-07-18', '2020-07-19', '2020-07-20', '2020-07-21', '2020-07-22', '2020-07-23', '2020-07-24', '2020-07-25', '2020-07-26', '2020-07-27', '2020-07-28', '2020-07-29', '2020-07-30', '2020-07-31', '2020-08-01', '2020-08-02', '2020-08-03', '2020-08-04', '2020-08-05', '2020-08-06', '2020-08-07', '2020-08-08', '2020-08-09', '2020-08-10', '2020-08-11', '2020-08-12', '2020-08-13', '2020-08-14', '2020-08-15', '2020-08-16', '2020-08-17', '2020-08-18', '2020-08-19', '2020-08-20', '2020-08-21', '2020-08-22', '2020-08-23', '2020-08-24', '2020-08-25', '2020-08-26', '2020-08-27', '2020-08-28', '2020-08-29', '2020-08-30', '2020-08-31']
print(max(l))

# 2020-08-31

或者,

import builtins as p

print(p.max(l))
# 2020-08-31

这篇关于在pyspark中查找列表的最大值/最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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