我有一个包含以下内容的数据框:
movieId / movieName / genre
1 example1 action|thriller|romance
2 example2 fantastic|action
Run Code Online (Sandbox Code Playgroud)
我想获取第二个数据帧(从第一个数据帧),其中包含以下内容:
movieId / movieName / genre
1 example1 action
1 example1 thriller
1 example1 romance
2 example2 fantastic
2 example2 action
Run Code Online (Sandbox Code Playgroud)
我们如何使用 pyspark 来做到这一点?
使用split函数将返回数组上的arraythenexplode函数。
Example:
df.show(10,False)
#+-------+---------+-----------------------+
#|movieid|moviename|genre |
#+-------+---------+-----------------------+
#|1 |example1 |action|thriller|romance|
#+-------+---------+-----------------------+
from pyspark.sql.functions import *
df.withColumnRenamed("genre","genre1").\
withColumn("genre",explode(split(col("genre1"),'\\|'))).\
drop("genre1").\
show()
#+-------+---------+--------+
#|movieid|moviename| genre|
#+-------+---------+--------+
#| 1| example1| action|
#| 1| example1|thriller|
#| 1| example1| romance|
#+-------+---------+--------+
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3383 次 |
| 最近记录: |