NZFunApply
Learn about the NZFunApply function.
This corresponds to use-case 1. Applying a custom data transformation on each and every record of the database table.
- Applying functions on each and every row of the table data
-
- Sample scenario: convert the MAXTEMP column in the weather table from Celsius to Fahrenheit.
- The user function that you want to run can assume two parameters by default,
self(which represents the AE context) andx(which represents the row of the table). You can usexto operate on the columns you’d like to. In the example, the third column (x[3]) was retrieved and processed further to generate a new value. For the columns that you would want to generate as output, build them as a list and useself.outputto populate the result. Alternatively, if you have only a single result, you can directly send it toself.output.from nzpyida import IdaDataBase, IdaDataFrame from nzpyida.ae import NZFunApply idadb = IdaDataBase('weather', 'admin', 'password', verbose=True) idadf = IdaDataFrame(idadb, 'WEATHER') def apply_fun(self, x): from math import sqrt max_temp = x[3] id = x[24] fahren_max_temp = (max_temp*1.8)+32 row = [id, max_temp, fahren_max_temp] self.output(row) output_signature = {'ID': 'int', 'MAX_TEMP': 'float', 'FAHREN_MAX_TEMP': 'float'} nz_apply = NZFunApply(df=idadf, fun_ref = code_str_apply, output_table="temp_conversion",output_signature=output_signature, merge_output_with_df=True) result = nz_apply.get_result() print(result)In Notebook environments, where you cannot send the function as reference, wrap your function in quotes and assign it to a string variable. If your function code is being sent as a string, you have to mention the function name in the
NZApplycall.Note: To generate indented AE code for the backend SQL function, you need to have the function name immediately after the quotes but not in the next line.Example:from nzpyida import IdaDataBase, IdaDataFrame from nzpyida.ae import NZFunApply idadb = IdaDataBase('weather', 'admin', 'password', verbose=True) idadf = IdaDataFrame(idadb, 'WEATHER') code_str_apply = """def apply_fun(self, x): from math import sqrt max_temp = x[3] id = x[24] fahren_max_temp = (max_temp*1.8)+32 row = [id, max_temp, fahren_max_temp] self.output(row) """ output_signature = {'ID': 'int', 'MAX_TEMP': 'float', 'FAHREN_MAX_TEMP': 'float'} nz_apply = NZFunApply(df=idadf, code_str=code_str_apply, fun_name='apply_fun', output_table="temp_conversion",output_signature=output_signature, merge_output_with_df=True) result = nz_apply.get_result() print(result)