how can I rewrite the code below to reduce memory usage as much as possible in…
- User
how can I rewrite the code below to reduce memory usage as much as possible in databricks? df_improvement_view = df_X_plus_5_status_quo.drop(F.col('VIEW'))
while df_improvements_X_plus_5.count() > 0:
view without duplicates
df_improvements_X_plus_5_temp_view = df_improvements_X_plus_5
.orderBy(['Unique_Concat_Value', 'IMPR_DATE']).dropDuplicates(subset=['Unique_Concat_Value'])Merge improved view with status quo df
df_X_plus_5_with_improvements = df_improvement_view
.join(df_improvements_X_plus_5_temp_view, on=['Unique_Concat_Value'], how='left')if df_X_plus_5_with_improvements.count() == df_improvement_view.count(): # Continue
# check columns for improved values -> if value is entered, keep value, else take the one from the standard view temp = df_X_plus_5_with_improvements \ .withColumn('ORGANIZATION_CODE', F.when((F.col('IMPR_Organization_Code').isNull()) | (F.isnan(F.col('IMPR_Organization_Code'))) | (F.col('IMPR_Organization_Code') == 0) | (F.col('PERIOD') < F.col('IMPR_DATE')), F.col('ORGANIZATION_CODE')) .otherwise(F.col('IMPR_Organization_Code')) ) \ .withColumn('TESTER_CODE', F.when((F.col('IMPR_TESTER_CODE').isNull()) | (F.col('PERIOD') < F.col('IMPR_DATE')), F.col('TESTER_CODE')) .otherwise(F.col('IMPR_TESTER_CODE')) ) \ .withColumn('TESTER_DESCRIPTION', F.when((F.col('IMPR_TESTER_DESCRIPTION').isNull()) | (F.col('PERIOD') < F.col('IMPR_DATE')), F.col('TESTER_DESCRIPTION')) .otherwise(F.col('IMPR_TESTER_DESCRIPTION')) ) \ .withColumn('RESOURCE_CODE', F.when((F.col('IMPR_RESOURCE_CODE').isNull()) | (F.col('PERIOD') < F.col('IMPR_DATE')), F.col('RESOURCE_CODE')) .otherwise(F.col('IMPR_RESOURCE_CODE')) ) \ .withColumn('RESOURCE_DESCRIPTION', F.when((F.col('IMPR_RESOURCE_DESCRIPTION').isNull()) | (F.col('PERIOD') < F.col('IMPR_DATE')), F.col('RESOURCE_DESCRIPTION')) .otherwise(F.col('IMPR_RESOURCE_DESCRIPTION')) ) \ .withColumn('PLANNING_YIELD', F.when((F.col('IMPR_PLANNING_YIELD').isNull()) | (F.col('PERIOD') < F.col('IMPR_DATE')), F.col('PLANNING_YIELD')) .otherwise(F.col('IMPR_PLANNING_YIELD')) ) \ .withColumn('REV_CUM_PLANNING_YIELD', F.when((F.col('IMPR_REV_CUM_PLANNING_YIELD').isNull()) | (F.col('PERIOD') < F.col('IMPR_DATE')), F.col('REV_CUM_PLANNING_YIELD')) .otherwise(F.col('IMPR_REV_CUM_PLANNING_YIELD')) ) \ .withColumn('EXEC_TIME', F.when((F.col('IMPR_EXEC_TIME').isNull()) | (F.col('PERIOD') < F.col('IMPR_DATE')), F.col('EXEC_TIME')) .otherwise(F.col('IMPR_EXEC_TIME')) ) \ .withColumn('SETUP_TIME', F.when((F.col('IMPR_SETUP_TIME').isNull()) | (F.col('PERIOD') < F.col('IMPR_DATE')), F.col('SETUP_TIME')) .otherwise(F.col('IMPR_SETUP_TIME')) ) \ .withColumn('INDEX_TIME', F.when((F.col('IMPR_INDEX_TIME').isNull()) | (F.col('PERIOD') < F.col('IMPR_DATE')), F.col('INDEX_TIME')) .otherwise(F.col('IMPR_INDEX_TIME')) ) \ .withColumn('PARALLEL_FACTOR', F.when((F.col('IMPR_PARALLEL_FACTOR').isNull()) | (F.col('PERIOD') < F.col('IMPR_DATE')), F.col('PARALLEL_FACTOR')) .otherwise(F.col('IMPR_PARALLEL_FACTOR')) ) \ .withColumn('RETEST_FACTOR', F.when((F.col('IMPR_RETEST_FACTOR').isNull()) | (F.col('PERIOD') < F.col('IMPR_DATE')), F.col('RETEST_FACTOR')) .otherwise(F.col('IMPR_RETEST_FACTOR')) ) \ .withColumn('SAMPLE_RATE', F.when((F.col('IMPR_SAMPLE_RATE').isNull()) | (F.col('PERIOD') < F.col('IMPR_DATE')), F.col('SAMPLE_RATE')) .otherwise(F.col('IMPR_SAMPLE_RATE')) ) \ .withColumn('PERF_FACTOR', F.when((F.col('IMPR_PERFORMANCE').isNull()) | (F.col('PERIOD') < F.col('IMPR_DATE')), F.col('PERF_FACTOR')) .otherwise(F.col('IMPR_PERFORMANCE')) ) # Recalculate below temp = temp \ .withColumn('CALC_TEST_TIME_DEVICE', (F.col('SETUP_TIME') + F.col('LEAD_TIME_LOT_SIZE') * (1 + F.col('RETEST_FACTOR')) * F.col('SAMPLE_RATE') * ((F.col('EXEC_TIME') + F.col('INDEX_TIME'))/F.col('PARALLEL_FACTOR')))/F.col('LEAD_TIME_LOT_SIZE') ) \ .withColumn('CALC_TEST_TIME_LOT', (F.col('SETUP_TIME') + F.col('LEAD_TIME_LOT_SIZE') * (1 + F.col('RETEST_FACTOR')) * F.col('SAMPLE_RATE') * ((F.col('EXEC_TIME') + F.col('INDEX_TIME'))/F.col('PARALLEL_FACTOR')))/3600 ) \ .withColumn('CALC_THROUGHPUT', 3600 / F.col('CALC_TEST_TIME_DEVICE')) \ .withColumn('CALC_TEST_TIME_DEVICE_PERF', F.col('CALC_TEST_TIME_DEVICE')/F.col('PERF_FACTOR')) \ .withColumn('CALC_TEST_TIME_LOT_PERF', F.col('CALC_TEST_TIME_LOT')/F.col('PERF_FACTOR')) \ .withColumn('CALC_THROUGHPUT_PERF', F.col('CALC_THROUGHPUT') * F.col('PERF_FACTOR')) # when resoure code changes from BACKEND to HANDLER, the resource group needs to be updated as well df_X_plus_5_with_improvements = temp \ .withColumn('RESOURCE_GROUP', F.when((F.col('IMPR_RESOURCE_CODE').isNull()) | (F.col('PERIOD') < F.col('IMPR_DATE')), F.col('RESOURCE_GROUP')) .when((F.col('IMPR_RESOURCE_CODE').substr(0,2) == 'P-') & (F.col('PERIOD') >= F.col('IMPR_DATE')), F.lit('PROBER')) .when((F.col('IMPR_RESOURCE_CODE').substr(0,2) == 'B-') & (F.col('PERIOD') >= F.col('IMPR_DATE')), F.lit('BACKEND')) .when((F.col('IMPR_RESOURCE_CODE').substr(0,2) == 'C-') & (F.col('PERIOD') >= F.col('IMPR_DATE')), F.lit('COMBI FT')) .when((F.col('IMPR_RESOURCE_CODE').substr(0,2) == 'H-') & (F.col('PERIOD') >= F.col('IMPR_DATE')), F.lit('HANDLER')) .when((F.col('IMPR_RESOURCE_CODE').substr(0,2) == 'M-') & (F.col('PERIOD') >= F.col('IMPR_DATE')), F.lit('HANDLER')) .otherwise(F.col('RESOURCE_GROUP')) ) # Add column with the view (Status Quo vs Improvements) df_improvement_view = df_X_plus_5_with_improvements.withColumn('VIEW', F.lit('Improvements')) # drop the columns that were joined from the IMPALA view as the columns will be added again if there are duplicates # (naming should remain the same to have a succesfull loop) col_to_drop = ('IMPR_Organization_Code', 'IMPR_RESOURCE_CODE', 'IMPR_RESOURCE_DESCRIPTION', 'IMPR_TESTER_CODE', 'IMPR_TESTER_DESCRIPTION', 'IMPR_PLANNING_YIELD', 'IMPR_REV_CUM_PLANNING_YIELD', 'IMPR_EXEC_TIME', 'IMPR_SETUP_TIME', 'IMPR_INDEX_TIME', 'IMPR_PARALLEL_FACTOR', 'IMPR_RETEST_FACTOR', 'IMPR_SAMPLE_RATE', 'IMPR_PERFORMANCE', 'IMPR_DATE', 'IMPR_REFERENCE', 'IMPR_STATUS', 'COMMENT') df_improvement_view = df_improvement_view.drop(*col_to_drop) - Assistant
- User
how to rewrite below pyspark code in a more efficient way? temp = df_X_plus_5_with_improvements
.withColumn('ORGANIZATION_CODE', F.when((F.col('IMPR_Organization_Code').isNull()) | (F.isnan(F.col('IMPR_Organization_Code'))) | (F.col('IMPR_Organization_Code') == 0) | (F.col('PERIOD') < F.col('IMPR_DATE')), F.col('ORGANIZATION_CODE')) .otherwise(F.col('IMPR_Organization_Code')) ) - Assistant
To make the code more efficient, you can use the
coalesce()function instead of multiplewhen()andotherwise()statements to avoid unnecessary column checks. Additionally, you can combine the conditions using logical operators for better performance. Here's how you can rewrite the code more efficiently:temp = df_X_plus_5_with_improvements.withColumn('ORGANIZATION_CODE', F.coalesce(F.when((F.col('IMPR_Organization_Code').isNull()) | (F.isnan(F.col('IMPR_Organization_Code'))) | (F.col('IMPR_Organization_Code') == 0) | (F.col('PERIOD') < F.col('IMPR_DATE')), F.col('ORGANIZATION_CODE')), F.col('IMPR_Organization_Code')) )By using
coalesce(), the function will return the first non-null value, eliminating the need for multiplewhen()andotherwise()statements. This simplifies the code and improves performance by reducing the number of operations performed.Note: If
IMPR_Organization_Codecan containNaNvalues, you might need to check forNaNusingisnan()before applying other conditions. - User
how can I write below code in pyspark in a more efficient way? df_X_plus_5_with_improvements = temp
.withColumn('RESOURCE_GROUP', F.when((F.col('IMPR_RESOURCE_CODE').isNull()) | (F.col('PERIOD') < F.col('IMPR_DATE')), F.col('RESOURCE_GROUP')) .when((F.col('IMPR_RESOURCE_CODE').substr(0,2) == 'P-') & (F.col('PERIOD') >= F.col('IMPR_DATE')), F.lit('PROBER')) .when((F.col('IMPR_RESOURCE_CODE').substr(0,2) == 'B-') & (F.col('PERIOD') >= F.col('IMPR_DATE')), F.lit('BACKEND')) .when((F.col('IMPR_RESOURCE_CODE').substr(0,2) == 'C-') & (F.col('PERIOD') >= F.col('IMPR_DATE')), F.lit('COMBI FT')) .when((F.col('IMPR_RESOURCE_CODE').substr(0,2) == 'H-') & (F.col('PERIOD') >= F.col('IMPR_DATE')), F.lit('HANDLER')) .when((F.col('IMPR_RESOURCE_CODE').substr(0,2) == 'M-') & (F.col('PERIOD') >= F.col('IMPR_DATE')), F.lit('HANDLER')) .otherwise(F.col('RESOURCE_GROUP')) ) - Assistant
To make the code more efficient, you can use a single
when()statement with multiple conditions instead of multiplewhen()statements. Additionally, you can useotherwise()to set a default value for each condition. Here's how you can rewrite the code more efficiently:df_X_plus_5_with_improvements = temp.withColumn('RESOURCE_GROUP', F.when((F.col('IMPR_RESOURCE_CODE').isNull()) | (F.col('PERIOD') < F.col('IMPR_DATE')), F.col('RESOURCE_GROUP')) .when((F.col('IMPR_RESOURCE_CODE').substr(0,2).isin('P-', 'B-', 'C-', 'H-', 'M-')) & (F.col('PERIOD') >= F.col('IMPR_DATE')), F.when(F.col('IMPR_RESOURCE_CODE').substr(0,2) == 'P-', F.lit('PROBER')) .when(F.col('IMPR_RESOURCE_CODE').substr(0,2) == 'B-', F.lit('BACKEND')) .when(F.col('IMPR_RESOURCE_CODE').substr(0,2) == 'C-', F.lit('COMBI FT')) .when(F.col('IMPR_RESOURCE_CODE').substr(0,2) == 'H-', F.lit('HANDLER')) .otherwise(F.lit('HANDLER'))) .otherwise(F.col('RESOURCE_GROUP')) ) - User
what is the best way in pyspark to update a value that changes based on a date? Basically I have a list with improvements which are linked to an improvement date. And I want them to be reflected in my source dataset one by one
- Assistant
To update a value in a PySpark DataFrame based on a date, you can use join or merge operations to link the improvement data with the source dataset using the corresponding date columns. Here's a general approach:
-
Create a DataFrame
improvementswith the improvement data that includes the improvement date (IMPR_DATE) and the column you want to update (IMPROVEMENT_COLUMN). -
Sort
improvementsby the improvement date in ascending order to reflect the updates in the correct order. -
Use a loop or iterative approach to update the source dataset
source_dfone improvement at a time. In each iteration:-
Get the current improvement information for the specific improvement date using
improvements. -
Join
source_dfwith the current improvement information based on the date
-
-