I have created a udf
, that aims to ffill
and bfill
a column, and return back a new imputed dataframe
. The error is not in the function as it works well.
See below my function:
def ffill_bfill(df,partition_by_col,order_by_col,col_to_imp):
'''Forward fill and Backward fill a column by a column/set of columns (order_col).
Parameters:
------------
df: Dataframe that the columns are in (Company wide? Company Narrow?)
order_col: String or List of string. This is the Year column until we get more granular time data!!
fill_col: String (Only work for a column). The name of the column to be imputed!!
Return:
---------
df: Dataframe
Return df with the filled_cols.
'''
# create the series containing the forward filled values
window_ff = Window.partitionBy(partition_by_col).orderBy(order_by_col).rowsBetween(-sys.maxsize, 0)
# create the series containing the backward filled values
window_bf = Window.partitionBy(partition_by_col).orderBy(order_by_col).rowsBetween(0, sys.maxsize)
# create the series containing the BACKWARD filled values for the two columns
s_bf = func.first(df[col_to_imp], ignorenulls=True).over(window_bf)
# create the series containing the FORWARD filled values for the two columns
s_ff = func.last(df[col_to_imp], ignorenulls=True).over(window_ff)
# add the IMPUTED column to a dataframe
imputed_df = df_company_wide.withColumn(f'{col_to_imp}_bf', s_bf)\
.withColumn(f'{col_to_imp}_ff', s_ff)
# Fill in the nulls with the imputed values
imputed_df = imputed_df.withColumn(f'{col_to_imp}_imp',coalesce(col_to_imp,f'{col_to_imp}_ff',f'{col_to_imp}_bf'))
# Create the imputed dataframes
cols_to_use = ['isin','company','year',col_to_imp]+[s for s in imputed_df.columns if col_to_imp in s and 'imp' in s]
imputed_df_final = imputed_df.select(cols_to_use)
return imputed_df_final
The issue is with the way I apply the function:
My intention is to apply the function, in 4 columns, and return back 4 imputed dataframes. I try to do that with the below code:
# Get the columns to be imputed in a list
features_to_impute = ['mobile_maximum_plan_for_one',
'mobile_minimum_plan_for_one',
'slowest_internet_speed',
'fastest_internet_speed']
# Return a dataframe and make available for SQL
for feature in features_to_impute:
f"{feature}_imp"= ffill_bfill(df_company_wide,partition_by_col='isin',order_by_col='year',col_to_imp=f"'{feature}'")
f"{feature}_imputed".createOrReplaceTempView(f"{feature}_imputed")
When I run the above command, I get the error:
SyntaxError: can't assign to literal
File "<command-575233896480136>", line 21
f"{feature}_imp"= ffill_bfill(df_company_wide,partition_by_col='isin',order_by_col='year',col_to_imp=f"'{feature}'")
^
SyntaxError: can't assign to literal
but when I try to apply the function, on 1 column at a time (like below), it works:
mobile_maximum_plan_for_one_imputed = ffill_bfill(df_company_wide,partition_by_col='isin',order_by_col='year',col_to_imp='mobile_maximum_plan_for_one')
mobile_minimum_plan_for_one_imputed.show()
+------------+----------------+------+---------------------------+-------------------------------+
| isin| company| year|mobile_minimum_plan_for_one|mobile_minimum_plan_for_one_imp|
+------------+----------------+------+---------------------------+-------------------------------+
|BE0003810273| Proximus|2015.0| null| 11.19820828667413|
|BE0003810273| Proximus|2016.0| null| 11.19820828667413|
|BE0003810273| Proximus|2017.0| null| 11.19820828667413|
|BE0003810273| Proximus|2018.0| null| 11.19820828667413|
|BE0003810273| Proximus|2019.0| 11.19820828667413| 11.19820828667413|
|CH0008742519| Swisscom|2015.0| null| 29.82|
|CH0008742519| Swisscom|2016.0| null| 29.82|
|CH0008742519| Swisscom|2017.0| null| 29.82|
|CH0008742519| Swisscom|2018.0| 29.82| 29.82|
|CH0008742519| Swisscom|2019.0| 29.82| 29.82|
Can someone shed some light on how to fix the for loop, to successful bring back the 4 different dataframes with the imputed values? A good explanation will add add a lot of value!
Many thanks in advance.