Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine multiple dictionaries into one pandas dataframe in long format

I have several dictionaries set up as follows:

Dict1 = {'Orange': ['1', '2', '3', '4']}
Dict2 = {'Red': ['3', '4', '5']}

And I'd like the output to be one combined dataframe:

| Type | Value |
|--------------|
|Orange|   1   |
|Orange|   2   |
|Orange|   3   |
|Orange|   4   |
| Red  |   3   |
| Red  |   4   |
| Red  |   5   |

I tried splitting everything out but I only get Dict2 in this dataframe.

mydicts = [Dict1, Dict2]
for x in mydicts:
    for k, v in x.items():
        df = pd.DataFrame(v)
        df['Type'] = k
like image 221
AGH_TORN Avatar asked Nov 19 '25 11:11

AGH_TORN


1 Answers

One option is using pd.concat:

pd.concat(map(pd.DataFrame, mydicts), axis=1).melt().dropna()

  variable value
0   Orange     1
1   Orange     2
2   Orange     3
3   Orange     4
4      Red     3
5      Red     4
6      Red     5

If performance matters, you can initialise a single DataFrame using DataFrame.from_dict and melt:

pd.DataFrame.from_dict({**Dict1, **Dict2}, orient='index').T.melt().dropna()
  variable value
0   Orange     1
1   Orange     2
2   Orange     3
3   Orange     4
4      Red     3
5      Red     4
6      Red     5

Or, using stack instead of melt (slightly slower, just for completeness):

res = (pd.DataFrame.from_dict({**Dict1, **Dict2}, orient='index').T
         .stack()
         .reset_index(level=1)
         .sort_values('level_1'))
res.columns = ['Type', 'Value']

print(res)
     Type Value
0  Orange     1
1  Orange     2
2  Orange     3
3  Orange     4
0     Red     3
1     Red     4
2     Red     5

The dictionary unpacking syntax works with python3.6. On older versions, replace {**d1, **d2} with {k: v for d in mydicts for k, v in d.items()}.

like image 155
cs95 Avatar answered Nov 21 '25 00:11

cs95



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!