My question is similar to this thread: Partitioning by multiple columns in Spark SQL
but I'm working in Pyspark rather than Scala and I want to pass in my list of columns as a list. I want to do something like this:
column_list = ["col1","col2"]
win_spec = Window.partitionBy(column_list)
I can get the following to work:
win_spec = Window.partitionBy(col("col1"))
This also works:
col_name = "col1"
win_spec = Window.partitionBy(col(col_name))
And this also works:
win_spec = Window.partitionBy([col("col1"), col("col2")])
You can also create partitions on multiple columns using PySpark partitionBy() . Just pass columns you want to partition as arguments to this method.
PySpark partitionBy() is used to partition based on column values while writing DataFrame to Disk/File system. When you write DataFrame to Disk by calling partitionBy() Pyspark splits the records based on the partition column and stores each partition data into a sub-directory.
Convert column names to column expressions with a list comprehension [col(x) for x in column_list]
:
from pyspark.sql.functions import col
column_list = ["col1","col2"]
win_spec = Window.partitionBy([col(x) for x in column_list])
Your first attempt should work.
Consider the following example:
import pyspark.sql.functions as f
from pyspark.sql import Window
df = sqlCtx.createDataFrame(
[
("a", "apple", 1),
("a", "orange", 2),
("a", "orange", 3),
("b", "orange", 3),
("b", "orange", 5)
],
["name", "fruit","value"]
)
df.show()
#+----+------+-----+
#|name| fruit|value|
#+----+------+-----+
#| a| apple| 1|
#| a|orange| 2|
#| a|orange| 3|
#| b|orange| 3|
#| b|orange| 5|
#+----+------+-----+
Suppose you wanted to calculate a fraction of the sum for each row, grouping by the first two columns:
cols = ["name", "fruit"]
w = Window.partitionBy(cols)
df.select(cols + [(f.col('value') / f.sum('value').over(w)).alias('fraction')]).show()
#+----+------+--------+
#|name| fruit|fraction|
#+----+------+--------+
#| a| apple| 1.0|
#| b|orange| 0.375|
#| b|orange| 0.625|
#| a|orange| 0.6|
#| a|orange| 0.4|
#+----+------+--------+
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With