aixd.data.utils_data.reformat_dict_to_dataframe
- aixd.data.utils_data.reformat_dict_to_dataframe(dct: Dict | List[Dict]) DataFrame [source]
Reformats data formatted as a (list of) dictionaries to a pandas dataframe. In the intended useage, the keys of the dictionary(ies) correspond to the original names of the data objects, and to column names in the dataframe.
Two formats of the input are possible and equivalent:
a list of dictionaries: each item corresponds to one sample, all dictionaries have to have the same keys.
- one dictionary: each key stores a list of values, each value corresponding to one sample.
The value itself can be a list if the object’s dimension is > 1.
- Parameters:
dct (Dict or List[Dict]) – A dictionary or a list of dictionaries.
- Returns:
pd.DataFrame – DataFrame object.
Examples
The following formats of the input are equivalent and produce the same dataframe:
>>> dct = [{"a": 0, "b": 1}, {"a": 2, "b": 3}] >>> dct = {"a": [0, 2], "b": [1, 3]} >>> reformat_dict_to_dataframe(dct) a b 0 0 1 1 2 3
If values itself are multi-dimensional, equally both formats are equivalent:
>>> dct = [{"a": [0, 0], "b": 1}, {"a": [2, 2], "b": 3}] >>> dct = {"a": [[0, 0], [2, 2]], "b": [1, 3]} >>> reformat_dict_to_dataframe(dct) a b 0 [0, 0] 1 1 [2, 2] 3