I have a large dataset (~4M rows by ~3K columns) and I'm currently mean-normalizing each column using the following code in Python/PySpark:
import pyspark.sql.functions as f
means_pd = df.select(*[f.mean(c).alias(c) for c in df.columns]).toPandas()
diffs = df
for c in df.columns:
mean = means_pd.loc[0,c]
diffs = diffs.withColumn(c, f.col(c) - f.lit(mean))
This is quite slow, particularly the step to loop over the columns. There must be a better way to do this since there are functions like MinMaxScalar that include a step like this but don't take forever. How can I speed this up?