1

I have a Spark dataframe column with trading pairs that I need to use to create a new column with the name of the coin populated in it.

The first column "bot" contains "Polkadot/USD", I need a new column called "coin" that contains only the substring "Polkadot" of the bot column. Same for all other rows. Basically the new column needs to have the substring "/USD" removed.

How would the code look like to accomplish this. I'm a crypto trader not a coder, so the more coding detail in the answer the better. Thank you.

Note: The notebook is a Python Notebook

enter image description here

2
  • Please add text instead of images to your question. Also, add anything that you have tried and issues, if you are facing any. Commented Jul 26, 2021 at 5:29
  • have you tried split() spark sql function? Commented Jul 26, 2021 at 7:19

1 Answer 1

1

You can use regexp_replace to substitute a substring with another substring

df.withColumn('coin', F.regexp_replace(F.col('bot'), '/USD', ''))

Example

# sample dataframe
df3 = spark.createDataFrame([
    ('BamBridge/USD', ),
    ('CLV/USD', ),
    ('ETH/USD', ),
    ('Polkadot/USD', ),
], ['bot'])

df3 = df3.withColumn('coin', F.regexp_replace(F.col('bot'), '/USD', ''))

df3.show()

+-------------+---------+
|          bot|     coin|
+-------------+---------+
|BamBridge/USD|BamBridge|
|      CLV/USD|      CLV|
|      ETH/USD|      ETH|
| Polkadot/USD| Polkadot|
+-------------+---------+
Sign up to request clarification or add additional context in comments.

2 Comments

Your example worked great. However I did have to remove the "F." from the two places in the command. After importing org.apache.spark.sql.functions.regexp_replace. the code line that worked is df3 = df3.withColumn('coin', regexp_replace(col('bot'), '/USD', '')) just had to remove the "F." thanks for your help.
It depends on whether you import the whole functions module as the shortcut F (as I did), or you import the necessary functions one by one (as you did). Please consider also upvoting the answer (in addition to accepting it) if you found it useful!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.