我每天都在更新 itemPrice
列的值 dailyRecords
在Dataframe中。下面是特定列的模式。
|-- dailyRecords: array (nullable = true)
| |-- element: struct (containsNull = true)
| | |-- dayId: integer (nullable = true)
| | |-- itemPrice: double (nullable = true)
| | |-- itemsPurchased: integer (nullable = true)
| | |-- itemSku: string (nullable = true)
(请注意,可能还有许多其他列)。我们可以假设dayid可以是一个从1递增到365的序列。
所以对于每个 itemSku
当 itemsPurchased is greater than 0
以及 itemPrice is equal to 0
然后更新 itemPrice
从 previous dayId
(例如:如果 dayId is 10
如果这个条件满足 itemPrice from dayId 9
),否则请离开 itemPrice
就这样继续下一步 dayId
我们将非常感谢任何通向理想解决方案或可能方法的线索
谢谢!
在考虑注解之后,当要更有效地更新多个列时,另一种替代解决方案是:
上面的架构 dailyRecords
通过组合来自 another dataframe
. 因此,考虑到评论,最好更新 itemPrice
当它位于一个单独的Dataframe中时,而不是在将列嵌套在一个列中之后执行此操作 dailyRecords
.
下面是我要更新的解决方案 itemPrice
值使用 foldLeft
.
https://stackoverflow.com/a/62307771/12322995
请注意我使用的这个解决方案 foldLeft
是因为我要更新的列比 itemPrice
在问题之外。
2条答案
按热度按时间ntjbwcob1#
最好在执行此操作之前在另一个df中生成正确的itemprice
struct
以及collect_list
如下图所示:然后使用
updatedPrice
从anotherDF
作为你的itemPrice
.hfyxw5xn2#
方法
dragPricesFromPreviousDay
获取两个参数,一个是要更新的列的列表,另一个是需要对其执行更新的Dataframe。我注意到了
.na.fill(0, Seq(priceCol))
因为如果它添加的列没有前一天的值null
当条件满足时。因此,我们可以通过用文本0或任何其他值填充空值来消除空值。