如何在Pyspark数据帧的另一列中获得具有多个列的值的列表列?

2024-04-20 03:39:02 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有一种方法可以创建一个新的列,如下面在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]|
+----+----+----+----+----+---------------+------+

Tags: 方法importdataframecolresultfunctionspysparkcol2
1条回答
网友
1楼 · 发布于 2024-04-20 03:39:02
# 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']

现在,最重要的部分是,我们使用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
))

最后,应用此映射来获取存储在第一列第二列中的列的值,并使用^{}将它们放入一个数组中。你知道吗

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]|
+  +  +  +  +  +      +   +

相关问题 更多 >