The data looks like: ID_device, ID_contract
The scenario, I need a new column filled with 0 or 1 based on the conditon if a ID_contract is assoziate with more than 1 (>) ID_device the column value for this ID_contract is 1 else its 0.
My approach was to iterate over all ID_contract values, take the row value as an condition in a where clause e.g. data.where(data.ID_contract == row value).count() to get the number of ID_devices for each ID_contract. This was done in a udf but it through an error saying its not allowed to perform dataframe calculations within a udf.
Any ideas how to solve this problem?
Great trhanks in advance!
This was my logic approach:
@udf(returnType=IntegerType())
def multidevicecontract(contractnr):
amount_devices = data.where(data.ID_contract == contractnr).count()
return amount_devices
data = data.withColumn("multidevicecontract", when(multidevicecontract(data.ID_contract) > 1,1).otherwise(0))
The exact error: PicklingError: Could not serialize object: TypeError: cannot pickle '_thread.RLock' object