PROBLEM: Convert filter(None, Dictionary)
to a dictionary again, while filtering empty keys.
I am trying to process some laboratory data using Pandas DataFrame. I am grouping this data, taking into account the first two columns, into groups by using dictionaries; see the following code:
d = {'col1': [1, 1, 1, 2], 'col2': [1, 2, 2, 2], 'col3': [1, 2 ,3, 4], 'col4': [2,1,1,1,], 'col5': [2,1,0,0], 'col6': [2,1,3,1]}
df = pd.DataFrame(data=d)
all_groups = {}
for i in df.col1.unique():
for j in df.col2.unique():
all_groups[f'{i}-{j}'] = df[(df.col1 == i) & (df.col2 == j)]
Then I would like to clean all the empty keys by using the function filter: filter(None, all_groups)
which appears to be as an answer in the following question:
[how to delete empty dict inside list of dictionary?
In the documentation it says:
Return an iterator yielding those items of iterable for which function(item) is true. If function is None, return the items that are true.
I need to convert this returned sequence to dictionary again but I do not know how because it is simply a filter object.
EDIT 1: I have tried with lambda functions:
hello = dict(filter(lambda x: len(x) != 0, all_groups))
ValueError: dictionary update sequence element #0 has length 12; 2 is required