首页 > 解决方案 > PySpark - Combine DF columns into named StructType

问题描述

I'm looking to combine multiple columns of a PySpark Data Frame into one column of the StructType.

Let's say I have a data frame like so:

columns = ['id', 'dogs', 'cats']
vals = [(1, 2, 0),(2, 0, 1)]
df = sqlContext.createDataFrame(vals, columns)

I'd like the resulting data frame to resemble this (not as it would actually be printed but to give an idea of what I mean if you aren't already familiar with StructType):

id | animals
1  | dogs=2, cats=0
2  | dogs=0, cats=1

Right now I am able to accomplish what I want with putting this:

StructType(
    [StructField('dogs', IntegerType(), True),
    [StructField('cats', IntegerType(), True)
)

at the end of my udfs, however, I'd rather just do it with a single function. I'd be surprised if one doesn't exist.

标签: pythondatabasedataframepyspark

解决方案


If you need a map column: create literal columns with the column names as keys and then use create_map function to construct the map column you needed:

from pyspark.sql.functions import create_map, lit
new_df = df.select(
    'id', 
     create_map(lit('dogs'), 'dogs', lit('cats'), 'cats').alias('animals')
     #                key  :  val,        key   :   val
)

new_df.show(2, False)
#+---+----------------------+
#|id |animals               |
#+---+----------------------+
#|1  |[dogs -> 2, cats -> 0]|
#|2  |[dogs -> 0, cats -> 1]|
#+---+----------------------+

new_df.printSchema()
#root
# |-- id: long (nullable = true)
# |-- animals: map (nullable = false)
# |    |-- key: string
# |    |-- value: long (valueContainsNull = true)

If you need a struct column: Use the struct function:

from pyspark.sql.functions import struct
new_df = df.select('id', struct('dogs', 'cats').alias('animals'))
new_df.show(2, False)
#+---+-------+
#|id |animals|
#+---+-------+
#|1  |[2, 0] |
#|2  |[0, 1] |
#+---+-------+

new_df.printSchema()
#root
# |-- id: long (nullable = true)
# |-- animals: struct (nullable = false)
# |    |-- dogs: long (nullable = true)
# |    |-- cats: long (nullable = true)

推荐阅读