Remove decimal value from pyspark column

Viewed 1849

I have a pyspark dataframe column where there are mix of values like some are string and some are numbers like below -

Source_ids
abc_123
1234.0
345
abc_cad
K-123
540.0
456.0

I want to remove the decimal part wherever it is coming. So result should be

Source_ids
abc_123
1234
345
abc_cad
K-123
540
456

We can not make this column long type since it has text also. How can I achieve it?

1 Answers

Using regexp_replace. Replace \..*$ with the empty string ''.

This expression finds the dot and replaces everything after that.

from pyspark.sql.functions import *
df.withColumn("Source_ids", regexp_replace('Source_ids', '\..*$', '')).show()
Related