1

I have the below Pyspark dataframe with Column_1 as string

S.No.      Column_1
1          1.0
2          1.0
3          2.0
4          N
5          N
6          3.0
7          N

I have to convert the numbers in column 1 from 1.0, 2.0 etc to just 1 or 2 or 3. I have to truncate the decimal places. I cannot cast it into int because it has string values too.

Expected Output:

S.No.      Column_1
1          1
2          1
3          2
4          N
5          N
6          3
7          N
0

1 Answer 1

3

Seems like you can use regexp_replace here:

df.withColumn("test",F.regexp_replace(F.col("Column_1"),'[.].?','')).show()

+-----+--------+----+
|S.No.|Column_1|test|
+-----+--------+----+
|    1|     1.0|   1|
|    2|     1.0|   1|
|    3|     2.0|   2|
|    4|       N|   N|
|    5|       N|   N|
|    6|     3.0|   3|
|    7|       N|   N|
+-----+--------+----+
Sign up to request clarification or add additional context in comments.

Comments

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.