pyspark - 获取最小值关联国家
问题描述
我有一个包含国家、地区、价值、产品的数据集。需要将 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))
需要一个带有区域国家的列,其 min_value 为 x。加入时无法获取国家和地区的值。
解决方案
我找到了解决方案。
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')))
推荐阅读
- webpack - 如何以角度使用 webpack“output.devtoolNamespace”
- vue.js - NUXT 插件 CSS 未在生产中加载
- accessibility - aria role="application" 和制表符
- reactjs - 无法获取数据 ReactJS
- python - Soup 可以在一个 IMBD 页面上工作,但不能在另一个页面上工作。怎么解决?
- apollo - ApolloGraphQL:使用订阅挂钩语法和 onSubscriptionData?
- scrapy - Scrapyd:如何检索爬虫或 scrapyd 项目的版本?
- node.js - Gibrish 获取图像并发送给客户端时
- c - OpenMP 嵌套循环跳过了一些组合
- aws-lambda - 有人在使用无服务器部署 lambda 函数时遇到问题吗?