我有一個pyspark資料框架:
Location Month Brand Sector TrueValue PickoutValue
美國 1/1/2021 brand1 cars1 7418 30000
美國 2/1/2021 brand1 cars1 1940 2000
美國 3/1/2021 brand1 cars1 4692 2900
美國 4/1/2021 brand1 cars1
美國 1/1/2021 brand2 cars2 16383104.2 16666667
美國 2/1/2021 brand2 cars2 26812874.2 166667
美國 3/1/2021 品牌2 汽車2
美國 1/1/2021 brand3 cars3 75.6% 70.0%
美國 3/1/2021 brand3 cars3 73.1% 70.0%
美國 2/1/2021 brand3 cars3 77.1% 70.0%
我為每個品牌準備了從2021年1月1日到2021年1月12日的月份值。我需要創建另一列,根據品牌和部門的TrueValue列的累積總和,并按月排序。 具有百分比值的行應該是累積和除以月數。
我預期的資料框架是:
Location Month Brand Sector TrueValue PickoutValue TotalSumValue
美國 1/1/2021 brand1 cars1 7418 30000 7418
美國 2/1/2021品牌1汽車1 1940 2000 9358
美國 3/1/2021品牌1 汽車1 4692 2900 14050
美國 4/1/2021 品牌1汽車1 14050
美國 1/1/2021 brand2 cars2 16383104. 2 16666667 16383104.
美國 2/1/2021 brand2 cars2 26812874. 2 16666667 43195978.4 43195978.
美國 3/1/2021 brand2 cars2 43195978.4
美國 1/1/2021 brand3 cars3 75. 6% 70.0% 75.6%
美國 3/1/2021品牌3 汽車3 73. 1% 70.0% 76.3%
美國 2/1/2021品牌3汽車3 77. 1% 70.0% 75.3%。
對于有%值的行,我需要這樣計算累計和按月排序:
(75.6 0)/1 = 75.6%
(75.6 77.1)/2 = 76.3%
(75.6 77.1 73.1)/3 = 75.3%
我能夠生成累積和,但我沒有得到%值的累積和。
這是我的代碼塊:
df=df.withColumn("month_in_timestamp", to_timestamp(df.month, 'dd/MM/yyyy'))
windowval = (Window.partitionBy('Brand','Sector').orderBy('Month')
.rangeBetween(Window.unboundedPreceding, 0)
df1 = df1.withColumn('TotalSumValue', F.sum('TrueValue').over(windowval))
uj5u.com熱心網友回復:
似乎帶%的值的計算是一個累積平均計算。如果是這樣,你可以對不包含%的值應用累積和,對有%的值應用累積平均(在計算前先去除百分比符號)。你可以使用when-otherwise來應用兩種計算方法。
import pyspark.sql.function as F
from pyspark.sql.window import Window
df = df.withColumn("month_in_timestamp", F.to_timestamp(F.col("month"), 'dd/MM/yyyy')
# use 'month_in_timestamp' instead of 'month' .
windowval = (Window.partitionBy('Brand','Sector').orderBy('month_in_timestamp')
.rangeBetween(Window.unboundedPreceding, 0)
df = df.withColumn("TotalSumValue",
F.when(F.col("TrueValue").包含("%")。
F.concat(F.avg(F.expr("replace(TrueValue, '%', '')").over(windowval).cast("十進制(4,1)"), F.lit("%")
.否則(F.sum('TrueValue').over(windowval).cast("十進制(13,1)")
df.顯示()
# -------- -------- ------ ------ ---------- ------------ ------------------- -------------
# |地點|月份|品牌|部門|真實價值|挑選價值|月份_in_timestamp|總和價值|。
# -------- -------- ------ ------ ---------- ------------ ------------------- -------------
# |美國|1/1/2021|brand1|cars1|7418|30000|2021-01-01 00:00:00|7418.0|。
# | 美國|2/1/2021|brand1| cars1| 1940| 2000|2021-01-02 00:00:00| 9358.0|。
# | 美國|3/1/2021|brand1| cars1| 4692| 2900|2021-01-03 00:00:00| 14050.0|
# | 美國|4/1/2021|brand1| cars1| null| null|2021-01-04 00:00:00| 14050.0|。
# | 美國|1/2021|brand2|carls2|16383104.2| 16666667|2021-01-01 00:00:00| 16383104.2|
# | 美國|2/1/2021|brand2|carls2|26812874.2| 16666667|2021-01-02 00:00:00| 43195978.4|。
# | 美國|3/1/2021|brand2| cars2| null| null|2021-01-03 00:00:00| 43195978.4|
# | 美國|1/1/2021|brand3| cars3| 75.6%| 70.0%|2021-01-01 00:00:00| 75.6%|。
# | 美國|2/1/2021|brand3| cars3| 77.1%| 70.0%|2021-01-02 00:00:00| 76.4%| >。
# | 美國|3/1/2021|brand3| cars3| 73.1%| 70.0%|2021-01-03 00:00:00| 75.3%| >。
# -------- -------- ------ ------ ---------- ------------ ------------------- -------------
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/310750.html
標籤:
