I'm working on Pyspark, created a sample dataframe with some long and decimal type columns. Here I wanted to fetch decimal type column value to two decimal points without rounding. Below is the code I tried.
df = spark.createDataFrame([
(324.456, "hi", "test"),
(453.987, "hello", "python"),
(768.66, "test", "java")
], ["col1", "col2", "col3"]
)
new = df.withColumn(
"col4",
F.substring((df.col1).cast(StringType()),1,F.instr((df.col1).cast(StringType()),".")+2))
So here I'm converting the column into string and finding he index position adding two (because I need two decimal points without rounding). But I don't what's the mistake here I'm getting Column object is not callable error. If I'm using only F.instr() function it is working fine. Kindly help with my other solution two fetch the value to two decimals without rounding.
Expected output
col1 col2 col3 col4
324.456 hi test 324.45
453.987 hello python 453.98
768.66 test java 768.66