Pyspark : 파이프로 구분 된 열을 여러 행으로 분할하는 방법은 무엇입니까? [복제]
Aug 18 2020
다음을 포함하는 데이터 프레임이 있습니다.
movieId / movieName / genre
1 example1 action|thriller|romance
2 example2 fantastic|action
다음을 포함하는 두 번째 데이터 프레임 (첫 번째 데이터 프레임에서)을 얻고 싶습니다.
movieId / movieName / genre
1 example1 action
1 example1 thriller
1 example1 romance
2 example2 fantastic
2 example2 action
pyspark를 사용하여 어떻게 할 수 있습니까?
답변
1 Shu Aug 18 2020 at 08:41
split
함수를 사용하면 배열에 array
then 함수가 반환 explode
됩니다.
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|
#+-------+---------+--------+