Forked from ayee/pyspark-split-dataframe-column-literal.py
Created
March 8, 2019 09:16
-
-
Save GermanCM/bea421b516a273be79ca82a99399bca0 to your computer and use it in GitHub Desktop.
Split Spark dataframe columns with literal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pyspark.sql.functions import split | |
df = sc.parallelize([[1, 'Foo:10'], [2, 'Bar:11'], [3,'Car:12']]).toDF(['Event', 'eventtype']) | |
df = df.withColumn('Thing', split(df.eventtype, ':')[0]) | |
df = df.withColumn('Ranking', split(df.eventtype, ':')[1]) | |
df.collect() | |
# [Row(Event=1, eventtype=u'Foo:10', Thing=u'Foo', Ranking=u'10'), | |
# Row(Event=2, eventtype=u'Bar:11', Thing=u'Bar', Ranking=u'11'), | |
# Row(Event=3, eventtype=u'Car:12', Thing=u'Car', Ranking=u'12')] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment