1

I am trying to populate a pandas dataframe from multiple dictionaries. Each of the dictionaries are in the form below:

{'Miami': {'DrPepper': '5', 'Pepsi': '8'}}

{'Atlanta:{'DrPepper':'10','Pepsi':'25'}}

Ultimately what I want is a dataframe that looks like this (After this I plan to use pandas to do some data transformations then output the dataframe to a tab delimited file):

      DrPepper  Pepsi
Miami    5       8
Atlanta  10      25

2 Answers 2

2

If you don't mind using an additional library, you can use toolz.merge to combine all of the dictionaries, followed by DataFrame.from_dict:

import toolz

d1 = {'Miami': {'DrPepper': '5', 'Pepsi': '8'}}
d2 = {'Atlanta': {'DrPepper': '10', 'Pepsi': '25'}}

df = pd.DataFrame.from_dict(toolz.merge(d1, d2), orient='index')

This method assumes that you don't have repeat index values (i.e city names). If you do, the repeats will be overwritten with the last one in the list of dictionaries taking precedence.

The resulting output:

        DrPepper Pepsi
Atlanta       10    25
Miami          5     8
Sign up to request clarification or add additional context in comments.

3 Comments

yet again, you've taught me something tremendously useful.
@piRSquared: Thanks, I've found that the toolz library contains tons of useful small functions. Seems like a package you'd enjoy. Also note that there's cytoolz, which is a cython implementation of toolz for enhanced performance
I have never heard of the toolz library, thanks for this recommendation!
2

You can use concat DataFrames created from dict by DataFrame.from_dict:

d1 = {'Miami': {'DrPepper': '5', 'Pepsi': '8'}}
d2 = {'Atlanta':{'DrPepper':'10','Pepsi':'25'}}

print (pd.DataFrame.from_dict(d1, orient='index'))
      Pepsi DrPepper
Miami     8        5

print (pd.concat([pd.DataFrame.from_dict(d1, orient='index'),
                  pd.DataFrame.from_dict(d2, orient='index')]))
        Pepsi DrPepper
Miami       8        5
Atlanta    25       10

Another solution with transpose by T:

print (pd.DataFrame(d1))
         Miami
DrPepper     5
Pepsi        8

print (pd.concat([pd.DataFrame(d1).T, pd.DataFrame(d2).T]))
        DrPepper Pepsi
Miami          5     8
Atlanta       10    25

Is possible use list comprehension also:

L = [d1,d2]
print (pd.concat([pd.DataFrame(d).T for d in L]))
        DrPepper Pepsi
Miami          5     8
Atlanta       10    25

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.