Ver código fonte

Add functionality to ignore certain labels from being flattend

ogert 4 anos atrás
pai
commit
2e386f0bee
1 arquivos alterados com 35 adições e 30 exclusões
  1. 35 30
      cdplib/FlattenData.py

+ 35 - 30
cdplib/FlattenData.py

@@ -62,10 +62,11 @@ class FlattenData():
                 "Parameter 'incoming_key' be of String type"
 
         result_dict = {}
-        if incoming_key not in labels_to_ignore:
-            for index, row in dataframe.iterrows():
-                temp_result_dict = {}
-                for key, value in row.iteritems():
+        
+        for index, row in dataframe.iterrows():
+            temp_result_dict = {}
+            for key, value in row.iteritems():
+                if key not in labels_to_ignore:
                     temp_result = {}
                     if incoming_key is not None:
                         key = incoming_key + '_' + key
@@ -76,10 +77,13 @@ class FlattenData():
                     else:
                         temp_result_dict[key] = value
 
-                    if len(temp_result) > 0:
+                else:
+                    temp_result_dict[key] = value
+
+                if len(temp_result) > 0:
                         temp_result_dict = self.append_to_dict(temp_result_dict, temp_result)
 
-                result_dict[index] = copy.deepcopy(temp_result_dict)
+            result_dict[index] = copy.deepcopy(temp_result_dict)
 
         return result_dict
 
@@ -96,9 +100,8 @@ class FlattenData():
 
 
         result_dict = {}
-        if incoming_key not in labels_to_ignore:
-            for key in dictionary:
-
+        for key in dictionary:
+            if key not in labels_to_ignore:
                 temp_dataframe = dictionary[key]
                 temp_result = {}
                 if incoming_key is not None:
@@ -109,8 +112,10 @@ class FlattenData():
                     temp_result = self.flatten_dict(temp_dataframe, key, labels_to_ignore)
                 else:
                     result_dict[key] = temp_dataframe
+            else:
+                result_dict[key] = temp_dataframe
 
-                if len(temp_result) > 0:
+            if len(temp_result) > 0:
                     result_dict = self.append_to_dict(result_dict, temp_result)
 
         return result_dict
@@ -133,30 +138,30 @@ class FlattenData():
             temp_dataframe = item
             temp_result = {}
             key = incoming_key
-            if incoming_key not in labels_to_ignore:
-                if incoming_key is not None:
-                    # OEBB SPECIFIC IF STATEMENT
-                    if type(data_list[iteration]) is dict and 'stationsnummer' in data_list[iteration].keys():
-                            key = incoming_key + '_' + str(data_list[iteration]['stationsnummer'])
-                    
-                    elif type(data_list[iteration]) is dict and 'stationsnummer' in data_list[iteration].keys() and 'stage' in data_list[iteration].keys() :
-                            key = incoming_key + '_' + str(data_list[iteration]['stationsnummer']) + '_' + str(data_list[iteration]['stage'])
-                    
-                    else:
-                        key = incoming_key + '_' + str(iteration)
+            
+            if incoming_key is not None:
+                # OEBB SPECIFIC IF STATEMENT
+                if type(data_list[iteration]) is dict and 'stationsnummer' in data_list[iteration].keys():
+                        key = incoming_key + '_' + str(data_list[iteration]['stationsnummer'])
+                
+                elif type(data_list[iteration]) is dict and 'stationsnummer' in data_list[iteration].keys() and 'stage' in data_list[iteration].keys() :
+                        key = incoming_key + '_' + str(data_list[iteration]['stationsnummer']) + '_' + str(data_list[iteration]['stage'])
+                
                 else:
-                    key = str(iteration)
-                if type(temp_dataframe) == list:
-                    temp_result = self.flatten_list(temp_dataframe, key, labels_to_ignore)
+                    key = incoming_key + '_' + str(iteration)
+            else:
+                key = str(iteration)
+            if type(temp_dataframe) == list:
+                temp_result = self.flatten_list(temp_dataframe, key, labels_to_ignore)
 
-                elif type(temp_dataframe) == dict:
-                    temp_result = self.flatten_dict(temp_dataframe, key, labels_to_ignore)
+            elif type(temp_dataframe) == dict:
+                temp_result = self.flatten_dict(temp_dataframe, key, labels_to_ignore)
 
-                else:
-                    result_dict[key] = temp_dataframe
+            else:
+                result_dict[key] = temp_dataframe
 
-                if len(temp_result) > 0:
-                    result_dict = self.append_to_dict(result_dict, temp_result)
+            if len(temp_result) > 0:
+                result_dict = self.append_to_dict(result_dict, temp_result)
 
         return result_dict