如何将pySpark中数据框的所有列与其他单列相乘
How to multiply all the columns of the dataframe in pySpark with other single column
我有特定年份从 1 月到 12 月所有月份的数据,我有一列说 "Constant",我需要将该常量列值乘以 1 月到 12 月的所有列数据在火花中。例如,我有以下数据:
JAN FEB MAR...DEC Constant
City1 160 158 253 391 12
City2 212 27 362 512 34
City3 90 150 145 274 56
乘法后,我想要 new/replace 数据框的值为:
JAN FEB MAR ....DEC
City1 192 1896 3036 1656
City2 7208 918 12308 8092
City3 504 280 8120 2464
我可以使用代码一次按一栏来完成:
Df.select("JAN","CONSTANT").withColumn("JAN",col('JAN') * col ('CONSTANT')).show()
有没有function/loop我可以在整个月份获得整个列乘法和新数据帧值的地方?
您可以使用 struct
的 structs
来表达您的逻辑。结构与column in higher order
基本相同,所以我们可以赋值them a name
,multiply them by constant
,然后 select 他们使用 columnname.*
。这样你就不必做 withColumn 12 times.
你可以把你所有的月份都放在 listofmonths
.
df.show() #sampledata
#+-----+---+---+---+---+--------+
#| City|JAN|FEB|MAR|DEC|Constant|
#+-----+---+---+---+---+--------+
#|City1|160|158|253|391| 12|
#|City2|212| 27|362|512| 34|
#|City3| 90|150|145|274| 56|
#+-----+---+---+---+---+--------+
listofmonths=['JAN','FEB','MAR','DEC']
from pyspark.sql import functions as F
df.withColumn("arr", F.struct(*[(F.col(x)*F.col('Constant')).alias(x) for x in listofmonths]))\
.select("City","arr.*")\
.show()
#+-----+----+----+-----+-----+
#| City| JAN| FEB| MAR| DEC|
#+-----+----+----+-----+-----+
#|City1|1920|1896| 3036| 4692|
#|City2|7208| 918|12308|17408|
#|City3|5040|8400| 8120|15344|
#+-----+----+----+-----+-----+
你也可以像这样使用 df.columns
而不是 listofmonths
:
from pyspark.sql import functions as F
df.withColumn("arr", F.struct(*[(F.col(x)*F.col('Constant')).alias(x) for x in df.columns if x!='City' and x!='Constant']))\
.select("City","arr.*")\
.show()
我有特定年份从 1 月到 12 月所有月份的数据,我有一列说 "Constant",我需要将该常量列值乘以 1 月到 12 月的所有列数据在火花中。例如,我有以下数据:
JAN FEB MAR...DEC Constant
City1 160 158 253 391 12
City2 212 27 362 512 34
City3 90 150 145 274 56
乘法后,我想要 new/replace 数据框的值为:
JAN FEB MAR ....DEC
City1 192 1896 3036 1656
City2 7208 918 12308 8092
City3 504 280 8120 2464
我可以使用代码一次按一栏来完成:
Df.select("JAN","CONSTANT").withColumn("JAN",col('JAN') * col ('CONSTANT')).show()
有没有function/loop我可以在整个月份获得整个列乘法和新数据帧值的地方?
您可以使用 struct
的 structs
来表达您的逻辑。结构与column in higher order
基本相同,所以我们可以赋值them a name
,multiply them by constant
,然后 select 他们使用 columnname.*
。这样你就不必做 withColumn 12 times.
你可以把你所有的月份都放在 listofmonths
.
df.show() #sampledata
#+-----+---+---+---+---+--------+
#| City|JAN|FEB|MAR|DEC|Constant|
#+-----+---+---+---+---+--------+
#|City1|160|158|253|391| 12|
#|City2|212| 27|362|512| 34|
#|City3| 90|150|145|274| 56|
#+-----+---+---+---+---+--------+
listofmonths=['JAN','FEB','MAR','DEC']
from pyspark.sql import functions as F
df.withColumn("arr", F.struct(*[(F.col(x)*F.col('Constant')).alias(x) for x in listofmonths]))\
.select("City","arr.*")\
.show()
#+-----+----+----+-----+-----+
#| City| JAN| FEB| MAR| DEC|
#+-----+----+----+-----+-----+
#|City1|1920|1896| 3036| 4692|
#|City2|7208| 918|12308|17408|
#|City3|5040|8400| 8120|15344|
#+-----+----+----+-----+-----+
你也可以像这样使用 df.columns
而不是 listofmonths
:
from pyspark.sql import functions as F
df.withColumn("arr", F.struct(*[(F.col(x)*F.col('Constant')).alias(x) for x in df.columns if x!='City' and x!='Constant']))\
.select("City","arr.*")\
.show()