0

I am translating Scala / Spark deep learning model into Python / PySpark. After reading the df all variables are interpreted as strings type. I need to cast them as float. Doing this one by one is easy, I think it would be like this:

format_number(result['V1'].cast('float'),2).alias('V1')

, but there is 31 columns How to do it all at once. The columns are "V1" to "V28" and "Time", "Amount", "Class"

Scala solution to it is this:

// cast all the column to Double type.
val df = raw.select(((1 to 28).map(i => "V" + i) ++ Array("Time", "Amount", "Class")).map(s => col(s).cast("Double")): _*)

https://github.com/intel-analytics/analytics-zoo/blob/master/apps/fraudDetection/Fraud%20Detction.ipynb

How to do the same in PySpark?

1 Answer 1

3

Use comprehensions:

result.select([
    format_number(result[c].cast('float'),2).alias(c) for c in result.columns
])
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.