【问题标题】:Getting the Min value associated country获取最小值关联国家
【发布时间】:2019-07-19 17:40:55
【问题描述】:

我有一个包含国家、地区、价值、产品的数据集。需要将 min_x 和 region-country 与 min-value 作为单独的列

数据集

cust    Country Region  value   product
 100    france  europe   1       x
 101    france  europe   2       x
 102    poland  europe   3       x
 103    poland  europe   3       y
 104    france  europe   4       y
 105    france  europe   5       y

我希望所有客户中每种产品的最低价值。为此,我按产品分组。

cust    Country Region  value   product min_x
 100    france  europe  1   x   1
 101    france  europe  2   x   1
 102    poland  europe  3   x   1
 103    poland  europe  3   y   3
 104    france  europe  4   y   3
 105    france  europe  5   y   3



 df = spark.read.csv('dataset',header=True)
 df1 = df.groupBy('Product').agg(min(df.value).alias('min_x))

还需要一个具有最小值为 x 的区域国家的列。加入时无法获取国家和地区的值。

【问题讨论】:

  • 与我链接的帖子的概念相同,但使用pyspark.sql.functions.min 而不是pyspark.sql.functions.count 作为聚合函数。
  • @pault,感谢您的回复,但是当我尝试使用窗口函数获取最小值时,我的问题是获取最小值产品的区域国家/地区。

标签: pyspark


【解决方案1】:

我找到了解决办法。

df = spark.read.csv(path,header=True)
w1 = Window.partitionBy(df.product).orderBy(df.value.desc())
df = df.withColumn('min_x',min(df.value).over(w1)).\
        withColumn('region_country',concat_ws('_',first('region'),first('country')))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-11
    • 1970-01-01
    • 2018-12-29
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多