Reputation: 3282
I have a column called code
This column contains 2 letter values. What I want to do is create another column called with_j
and in that column I want all codes that end with j. However, instead of it saying the actual code, I want this: *j
So something that looks like this:
Before:
code
bx
aj
dj
la
After
code with_j
bx
aj *j
dj *j
la
Is there a way I can do this?
Upvotes: 0
Views: 39
Reputation: 10406
It's actually very simple with endsWith
and when/otherwise
.
Seq("bx", "aj", "dj", "la")
.toDF("code")
.withColumn("with_j",
when('code endsWith "j", "*j").otherwise("")
)
.show
Upvotes: 1