Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在pyspark中解压列表类型的列_Python_Apache Spark_Pyspark_Apache Spark Sql - Fatal编程技术网

Python 如何在pyspark中解压列表类型的列

Python 如何在pyspark中解压列表类型的列,python,apache-spark,pyspark,apache-spark-sql,Python,Apache Spark,Pyspark,Apache Spark Sql,我在pyspark中有一个dataframe,df有一个类型为array string的列,因此我需要生成一个新列,该列具有列表的开头,并且我还需要其他列具有尾部列表的concat 这是我的原始数据帧: pyspark> df.show() +---+------------+ | id| lst_col| +---+------------+ | 1|[a, b, c, d]| +---+------------+ pyspark> df.printSchema()

我在pyspark中有一个dataframe,df有一个类型为array string的列,因此我需要生成一个新列,该列具有列表的开头,并且我还需要其他列具有尾部列表的concat

这是我的原始数据帧:

pyspark> df.show()
+---+------------+
| id|     lst_col|
+---+------------+
|  1|[a, b, c, d]|
+---+------------+


pyspark> df.printSchema()
root
 |-- id: integer (nullable = false)
 |-- lst_col: array (nullable = true)
 |    |-- element: string (containsNull = true)
我需要生成如下内容:

pyspark> df2.show()
+---+--------+---------------+
| id|lst_head|lst_concat_tail|
+---+--------+---------------+
|  1|       a|          b,c,d|
+---+--------+---------------+

对于Spark 2.4+,您可以对阵列使用
element_at
slice
size
功能:

df.select("id",
          element_at("lst_col", 1).alias("lst_head"),
          expr("slice(lst_col, 2, size(lst_col))").alias("lst_concat_tail")
         ).show()
给出:

+---+--------+---------------+
| id|lst_head|lst_concat_tail|
+---+--------+---------------+
|  1|       a|      [b, c, d]|
+---+--------+---------------+

对于Spark 2.4+,您可以对阵列使用
element_at
slice
size
功能:

df.select("id",
          element_at("lst_col", 1).alias("lst_head"),
          expr("slice(lst_col, 2, size(lst_col))").alias("lst_concat_tail")
         ).show()
给出:

+---+--------+---------------+
| id|lst_head|lst_concat_tail|
+---+--------+---------------+
|  1|       a|      [b, c, d]|
+---+--------+---------------+