Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
260 views
in Technique[技术] by (71.8m points)

apache spark - Pyspark - Looking to create a normalized version of a Double column

As the title states, I'd like to create a normalized version of an existing Double column.

As I'm quite new to pyspark, this was my attempt at solving this:

df2 = df.groupBy('id').count().toDF(*['id','count_trans'])
df2 = df2.withColumn('count_trans_norm', F.col('count_trans) / (F.max(F.col('count_trans'))))

When I do this, I get the following error:

"grouping expressions sequence is empty, and '`movie_id`' is not an aggregate function.

Any help would be much appreciated.

question from:https://stackoverflow.com/questions/65925426/pyspark-looking-to-create-a-normalized-version-of-a-double-column

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to specify an empty window if you want to get the maximum of count_trans in df2:

df2 = df.groupBy('id').count().toDF(*['id','count_trans'])
df3 = df2.selectExpr('*', 'count_trans / max(count_trans) over () as count_trans_norm')

Or if you prefer pyspark syntax:

from pyspark.sql import functions as F, Window

df3 = df2.withColumn('count_trans_norm', F.col('count_trans') / F.max(F.col('count_trans')).over(Window.orderBy()))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...