有没有办法创建一个新列,例如下面在Pyspark中显示的Dataframe?
我一直在尝试列表理解:
import pyspark.functions as F
df.withColumn('result', [F.col(colname) for colname in F.col('colList')])
但是不起作用。
预期结果是:
+----+----+----+----+----+---------------+------+
|col1|col2|col3|col4|col5| colList|result|
+----+----+----+----+----+---------------+------+
| 1| 2| 0| 3| 4|['col1','col2']| [1,2]|
| 1| 2| 0| 3| 4|['col2','col3']| [2,0]|
| 1| 2| 0| 3| 4|['col1','col3']| [1,0]|
| 1| 2| 0| 3| 4|['col3','col4']| [0,3]|
| 1| 2| 0| 3| 4|['col2','col5']| [2,4]|
| 1| 2| 0| 3| 4|['col4','col5']| [3,4]|
+----+----+----+----+----+---------------+------+
答案 0 :(得分:0)
# Loading requisite functions and creating the DataFrame
from pyspark.sql.functions import create_map, lit, col, struct
from itertools import chain
myValues = [(1,2,0,3,4,['col1','col2']),(1,2,0,3,4,['col2','col3']),
(1,2,0,3,4,['col1','col3']),(1,2,0,3,4,['col3','col4']),
(1,2,0,3,4,['col2','col5']),(1,2,0,3,4,['col4','col5'])]
df = sqlContext.createDataFrame(myValues,['col1','col2','col3','col4','col5','colList'])
df.show()
+----+----+----+----+----+------------+
|col1|col2|col3|col4|col5| colList|
+----+----+----+----+----+------------+
| 1| 2| 0| 3| 4|[col1, col2]|
| 1| 2| 0| 3| 4|[col2, col3]|
| 1| 2| 0| 3| 4|[col1, col3]|
| 1| 2| 0| 3| 4|[col3, col4]|
| 1| 2| 0| 3| 4|[col2, col5]|
| 1| 2| 0| 3| 4|[col4, col5]|
+----+----+----+----+----+------------+
下一步,我们为colList数组中的各个列创建列。
df = df.withColumn('first_col',col('colList')[0])
df = df.withColumn('second_col',col('colList')[1])
df.show()
+----+----+----+----+----+------------+---------+----------+
|col1|col2|col3|col4|col5| colList|first_col|second_col|
+----+----+----+----+----+------------+---------+----------+
| 1| 2| 0| 3| 4|[col1, col2]| col1| col2|
| 1| 2| 0| 3| 4|[col2, col3]| col2| col3|
| 1| 2| 0| 3| 4|[col1, col3]| col1| col3|
| 1| 2| 0| 3| 4|[col3, col4]| col3| col4|
| 1| 2| 0| 3| 4|[col2, col5]| col2| col5|
| 1| 2| 0| 3| 4|[col4, col5]| col4| col5|
+----+----+----+----+----+------------+---------+----------+
具有整数值的列的列表-
concerned_columns = [x for x in df.columns if x not in {'colList','first_col','second_col'}]
print(concerned_columns)
['col1', 'col2', 'col3', 'col4', 'col5']
现在,最重要的部分是,我们使用create_map
函数创建了列名称及其相应值之间的映射,此函数已在Spark 2. +及更高版本中提供。
# Maping - (column name, column values)
col_name_value_mapping = create_map(*chain.from_iterable(
(lit(c), col(c)) for c in concerned_columns
))
最后,应用此映射来获取存储在列 first_col 和 second_col 中的列的值,并使用struct
将它们放入数组中。
df = df.withColumn('result', struct(col_name_value_mapping[col('first_col')],col_name_value_mapping[col('second_col')]))
df = df.drop('first_col','second_col')
df.show()
+----+----+----+----+----+------------+------+
|col1|col2|col3|col4|col5| colList|result|
+----+----+----+----+----+------------+------+
| 1| 2| 0| 3| 4|[col1, col2]| [1,2]|
| 1| 2| 0| 3| 4|[col2, col3]| [2,0]|
| 1| 2| 0| 3| 4|[col1, col3]| [1,0]|
| 1| 2| 0| 3| 4|[col3, col4]| [0,3]|
| 1| 2| 0| 3| 4|[col2, col5]| [2,4]|
| 1| 2| 0| 3| 4|[col4, col5]| [3,4]|
+----+----+----+----+----+------------+------+