python - Concatenating iterable of defaultdicts into DataFrame -


simplified example of have now:

from collections import defaultdict  d1 = defaultdict(list) d2 = defaultdict(list)  d1['a'] = [1, 2, 3] d1['b'] = [true, true, true]  d2['a'] = [4, 5 , 6] d2['b'] = [false, false, false] 

desired result:

        b 0  1   true 1  2   true 2  3   true 3  4  false 4  5  false 5  6  false 

this line below work, i'm looking alternative doesn't have instantiate separate dataframe every defaultdict.

pd.concat([pd.dataframe(d) d in (d1, d2)]).reset_index(drop=true) 

could start with:

pd.dataframe([d1, d2]) 

and convert long format.

you merge dicts , then instantiate dataframe.

d3 = {k : d1[k] + d2[k] k in d1} d3 {'a': [1, 2, 3, 4, 5, 6], 'b': [true, true, true, false, false, false]}  df = pd.dataframe(d3) df         b 0  1   true 1  2   true 2  3   true 3  4  false 4  5  false 5  6  false 

automating merge multiple objects:

d3 = defaultdict(list) d in dict_list:     k in d:         d3[k].extend(d[k])  df = pd.dataframe(d3) 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -