4

I am trying to get dataFrame from this function :

def total_sum(self):
    c = defaultdict(int)
    for slot in self.data:
        c[slot['accountLabelType']] += slot['totalPrice']
    return(c)

it returns a variable that contains a whole dict with a key:value structure.

When I try to create the panda's dataframe like this :

def source_revenue(self):
        # df = pandas.DataFrame(self.data, columns=[
                            #   'Source Of Business', 'Revenue'])
        df = pandas.DataFrame({'CASH' : self.data})
        print(df)

I get this :

                                  CASH
BYD - Other                      500.0
BYD - Retail                    1584.0

But i want it to be like :

SOURCE                            CASH
BYD - Other                      500.0
BYD - Retail                    1584.0

But I can't do df = pandas.DataFrame({'SOURCE : self.data[0]'CASH' : self.data[1})

Because it is a dictionary, how do I properly extract both values so I can create the dataframe ?

sample output dictionary :

defaultdict(<class 'int'>, {'Spa': 3052, 'GS - Retail': 386, 'SCH Beverage - A La Carte': 119, 'BYD - Retail': 1584, 'BYD - Transport': 42498, 'BYD - Other': 500, 'BYD Food - Catering Banquet': 53796, 'Orchard Retail': 130, 'SCH - Retail': 375.4, 'SCH - Transport': 888, 'BYD Food - A La Carte 瓦厂食品-零点': 68365, 'XLM Beverage - A La Carte': 38, 'GS Food - A La Carte': 48, 'BYD Rooms 瓦厂房间': 5148, 'BYD Beverage - A La Carte': 39401.5, 'SCH - Food - A La Carte': 96})

3 Answers 3

5

You could just put your dictionary into a list:

import pandas as pd
some_dict = {'a': 1, 'b': 2, 'c': 11, 'd': 66}  
df = pd.DataFrame([some_dict])
Sign up to request clarification or add additional context in comments.

1 Comment

This works and is by far the simplest answer. This answer should be much higher.
2

You have to convert the dictionary to two arrays, one for keys and other for values:

data = {
    'test1': 10,
    'test2': 20,
    'testx': 40,
    'foo': 50,
}

items = data.items()
pd.DataFrame({'keys': [i[0] for i in items], 'values': [i[1] for i in items]})

Assigning variable items is important because order may not be deterministic.

2 Comments

Works perfectly. Thank you !
This might be simpler: df = pd.DataFrame({'keys': data.keys(), 'values': data.values()})
2

Ok first, the values should be lists: let's say your dictionary is dico, first convert values to lists:

dico = { x:[y] for x,y in dico.iteritems() }

Then build your dataframe:

df = pandas.DataFrame.from_dict(dico)

2 Comments

ValueError: If using all scalar values, you must pass an index
can you print a sample of your dictionary?

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.