user2896120
user2896120

Reputation: 3282

How to make a column based on another column's value?

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

Answers (1)

Oli
Oli

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

Related Questions