How to split Vector into columns – using PySpark [duplicate]

Spark >= 3.0.0 Since Spark 3.0.0 this can be done without using UDF. from pyspark.ml.functions import vector_to_array (df .withColumn(“xs”, vector_to_array(“vector”))) .select([“word”] + [col(“xs”)[i] for i in range(3)])) ## +——-+—–+—–+—–+ ## | word|xs[0]|xs[1]|xs[2]| ## +——-+—–+—–+—–+ ## | assert| 1.0| 2.0| 3.0| ## |require| 0.0| 2.0| 0.0| ## +——-+—–+—–+—–+ Spark < 3.0.0 One possible approach is to … Read more

Difference between df.repartition and DataFrameWriter partitionBy?

Watch out: I believe the accepted answer is not quite right! I’m glad you ask this question, because the behavior of these similarly-named functions differs in important and unexpected ways that are not well documented in the official spark documentation. The first part of the accepted answer is correct: calling df.repartition(COL, numPartitions=k) will create a … Read more

Errors when using OFF_HEAP Storage with Spark 1.4.0 and Tachyon 0.6.4

There seems to be a related bug report: https://issues.apache.org/jira/browse/SPARK-10314 Since there seems to be a pull request for this, there might be a chance to soon get a fix for this. From this thread, https://groups.google.com/forum/#!topic/tachyon-users/xb8zwqIjIa4, it looks like Spark is using TRY_CACHE mode to write to Tachyon so the data seems to get lost when … Read more

Converting Pandas dataframe into Spark dataframe error

I made this script, It worked for my 10 pandas Data frames from pyspark.sql.types import * # Auxiliar functions def equivalent_type(f): if f == ‘datetime64[ns]’: return TimestampType() elif f == ‘int64’: return LongType() elif f == ‘int32’: return IntegerType() elif f == ‘float64’: return DoubleType() elif f == ‘float32’: return FloatType() else: return StringType() def … Read more

Pyspark: Filter dataframe based on multiple conditions

Your logic condition is wrong. IIUC, what you want is: import pyspark.sql.functions as f df.filter((f.col(‘d’)<5))\ .filter( ((f.col(‘col1’) != f.col(‘col3’)) | (f.col(‘col2’) != f.col(‘col4’)) & (f.col(‘col1’) == f.col(‘col3’))) )\ .show() I broke the filter() step into 2 calls for readability, but you could equivalently do it in one line. Output: +—-+—-+—-+—-+—+ |col1|col2|col3|col4| d| +—-+—-+—-+—-+—+ | A| … Read more

Why does join fail with “java.util.concurrent.TimeoutException: Futures timed out after [300 seconds]”?

This happens because Spark tries to do Broadcast Hash Join and one of the DataFrames is very large, so sending it consumes much time. You can: Set higher spark.sql.broadcastTimeout to increase timeout – spark.conf.set(“spark.sql.broadcastTimeout”, newValueForExample36000) persist() both DataFrames, then Spark will use Shuffle Join – reference from here PySpark In PySpark, you can set the … Read more