From 734ff0ea8ed7a3e50a88758505667730447220b6 Mon Sep 17 00:00:00 2001 From: geohouse Date: Thu, 2 Jul 2020 18:00:16 -0600 Subject: [PATCH] Adding functional For loop Try/Catch example code Hi Marcela, this is a working example of complex indexing that happens in the for loop and try blocks of the R SDG formatting code. I left in lines that helped me put the indexing together in pieces, and hopefully can help you understand the pieces of the long indexing call. These lines have "#FOR DEMO" written above them. It turns out the .item() calls at the end of some of the expressions are critical to prevent the mis-matched indexing errors we were getting earlier (they access the actual elements in the data structures returned by the expressions). The 'try' blocks are also essential I realized, because the lookup fails if a field collected sample has not yet been processed by the lab. To incorporate this into the Python function, remove the #FOR DEMO lines, and add another indexing/assignment line with the correct column names substituted in the try block (so there should be a total of 2 assignment lines within each try block). Then make the 3 different try blocks like in the R code. Let me know if you have any questions! --- SDG_FORLOOP_WITHTRYCATCH_TEMPLATE.PY | 31 ++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 SDG_FORLOOP_WITHTRYCATCH_TEMPLATE.PY diff --git a/SDG_FORLOOP_WITHTRYCATCH_TEMPLATE.PY b/SDG_FORLOOP_WITHTRYCATCH_TEMPLATE.PY new file mode 100644 index 0000000..707c4ec --- /dev/null +++ b/SDG_FORLOOP_WITHTRYCATCH_TEMPLATE.PY @@ -0,0 +1,31 @@ +for x in range(len(outputDF['waterSampleID'])): + # print(x) + # FOR DEMO + holder = outputDF.loc[outputDF.index[[x]], 'referenceAirSampleID'] + # FOR DEMO + holder2 = externalLabData.loc[:, 'sampleID'] + # Need to actually access the single cell value (string) in holder using .item() + # FOR DEMO + booleanHolder = holder2 == holder.item() + # FOR DEMO + holder3 = externalLabData.loc[booleanHolder, 'concentrationCO2'] + + # This is all of the above intermediate variables put together into one line, and is the right hand side of the + # assignment into the 'concentrationCO2Air' column of the outputDF data frame (at row 'x') + # FOR DEMO + rhsOfAssignment = externalLabData.loc[externalLabData.loc[:, 'sampleID'] == outputDF.loc[ + outputDF.index[[x]], 'referenceAirSampleID'].item(), 'concentrationCO2'] + + # This needs to be in a try block because the outputDF has sampleIDs that are populated from the field samples, + # and this is looping through those sampleIDs to look for matches with the lab data, but for recently + # collected samples, there won't be lab data for them (but will obviously still be field samples) so this + # look-up will fail. Put it in a try block with nothing in the except block to imitate the silent try + # that this code has in R + try: + outputDF.loc[outputDF.index[[x]], 'concentrationCO2Air'] = externalLabData.loc[ + externalLabData.loc[:, 'sampleID'] == outputDF.loc[ + outputDF.index[[x]], 'referenceAirSampleID'].item(), 'concentrationCO2'].item() + # NEEDS THE OTHER ASSIGNMENT LINE HERE (THERE ARE 2 ASSIGNMENT LINES WITHIN EACH TRY BLOCK IN THE R CODE) + + except Exception: + pass