|
@@ -20,7 +20,7 @@ class FlattenData():
|
|
def __init__(self):
|
|
def __init__(self):
|
|
self._log = Log("Flatten data")
|
|
self._log = Log("Flatten data")
|
|
|
|
|
|
- def flatten(self, data, labels_to_ignore: list = None) -> pd.DataFrame():
|
|
|
|
|
|
+ def flatten(self, data, labels_to_ignore: list = []) -> pd.DataFrame():
|
|
'''
|
|
'''
|
|
:parm data: data given in either dictionary, list or dataframe format.
|
|
:parm data: data given in either dictionary, list or dataframe format.
|
|
'''
|
|
'''
|
|
@@ -50,7 +50,7 @@ class FlattenData():
|
|
self._log.info(('Data has been flattened, created {} columns in {} seconds').format(len(result_dataframe.columns)- in_length, time.time()-start))
|
|
self._log.info(('Data has been flattened, created {} columns in {} seconds').format(len(result_dataframe.columns)- in_length, time.time()-start))
|
|
return result_dataframe
|
|
return result_dataframe
|
|
|
|
|
|
- def flatten_dataframe(self, dataframe: pd.DataFrame, incoming_key: str = None, labels_to_ignore: list = None):
|
|
|
|
|
|
+ def flatten_dataframe(self, dataframe: pd.DataFrame, incoming_key: str = None, labels_to_ignore: list = []):
|
|
'''
|
|
'''
|
|
:param pd.Dataframe dataframe: dataframe containing the data to be flattened
|
|
:param pd.Dataframe dataframe: dataframe containing the data to be flattened
|
|
:param str incoming_key: string to be appended to the key
|
|
:param str incoming_key: string to be appended to the key
|
|
@@ -66,6 +66,9 @@ class FlattenData():
|
|
for index, row in dataframe.iterrows():
|
|
for index, row in dataframe.iterrows():
|
|
temp_result_dict = {}
|
|
temp_result_dict = {}
|
|
for key, value in row.iteritems():
|
|
for key, value in row.iteritems():
|
|
|
|
+ print('key:', key)
|
|
|
|
+ print('value:', value)
|
|
|
|
+ print('labels_to_ignore:', labels_to_ignore)
|
|
if key not in labels_to_ignore:
|
|
if key not in labels_to_ignore:
|
|
temp_result = {}
|
|
temp_result = {}
|
|
if incoming_key is not None:
|
|
if incoming_key is not None:
|
|
@@ -87,7 +90,7 @@ class FlattenData():
|
|
|
|
|
|
return result_dict
|
|
return result_dict
|
|
|
|
|
|
- def flatten_dict(self, dictionary: dict, incoming_key: str = None, labels_to_ignore: list = None):
|
|
|
|
|
|
+ def flatten_dict(self, dictionary: dict, incoming_key: str = None, labels_to_ignore: list = []):
|
|
'''
|
|
'''
|
|
:param dict dictionary: dictionary containing the data to be flattened
|
|
:param dict dictionary: dictionary containing the data to be flattened
|
|
:param str incoming_key: string to be appended to the key
|
|
:param str incoming_key: string to be appended to the key
|
|
@@ -120,7 +123,7 @@ class FlattenData():
|
|
|
|
|
|
return result_dict
|
|
return result_dict
|
|
|
|
|
|
- def flatten_list(self, data_list: list, incoming_key: str = None, labels_to_ignore: list = None):
|
|
|
|
|
|
+ def flatten_list(self, data_list: list, incoming_key: str = None, labels_to_ignore: list = []):
|
|
'''
|
|
'''
|
|
:param list data_list: list containing the data to be flattened
|
|
:param list data_list: list containing the data to be flattened
|
|
:param str incoming_key: string to be appended to the key
|
|
:param str incoming_key: string to be appended to the key
|
|
@@ -180,11 +183,11 @@ class FlattenData():
|
|
|
|
|
|
return dictionary
|
|
return dictionary
|
|
|
|
|
|
- def flatten_if_not_flat(self, data: pd.DataFrame, labels_to_ignore: list = None):
|
|
|
|
|
|
+ def flatten_if_not_flat(self, data: pd.DataFrame, labels_to_ignore: list = []):
|
|
|
|
|
|
for data_type in data.dtypes:
|
|
for data_type in data.dtypes:
|
|
if data_type == object:
|
|
if data_type == object:
|
|
- return self.flatten(data)
|
|
|
|
|
|
+ return self.flatten(data, labels_to_ignore)
|
|
|
|
|
|
return data
|
|
return data
|
|
|
|
|