Append dataframe to dict
<p>I've created a dict of dicts structured in such a way that the key is the department ('ABC') then the date (01.08) is the key and values are { product name (A), Units (0), Revenue (0)}. This structure continues for several departments. See dict of dict printout below. </p>

<pre><code>'ABC':                                                                   01.08  \


A.         Units           0
                                          Revenue               0
B.         Units           0
                                          Revenue               0
C.         Units           0
                                          Revenue               0
D.         Units           0
                                          Revenue               0
</code></pre>

<p>Additionally, I've created a dataframe using groupby and an aggregation function (sum) to get the total of units and revenue per day per department (this is an aggregation of two levels as opposed to three in the dict - date , department, product).</p>

<p>Printing out df, which is an aggregation of number of units and total revenue, results in:                                               </p>

<pre><code>print df.ix['ABC']

                        Total Overall Units  \
dates
2016-08-01                   2
2016-08-02                   0
2016-08-03                   2
2016-08-04                   1
2016-08-22                   2

                       Total Overall Revenue \
dates
2016-08-01              20
2016-08-02              500
2016-08-03              39
2016-08-04              50
</code></pre>

<p>I am currently ending up with two separate objects which I want to merge/append such that the total units and total revenue will be added to the end of the dict in the correct place (i.e. mapped to the correct department and date).
Currently I am printing the dict and then the dataframe <code>pd.to html</code> separately by 'department' so I am left with two separate tables. Not only are they separate even but the table created from the df also has one fewer column as they are grouped differently.</p>

<pre><code>'ABC':
                         01.08  |  02.08  |   03.08  |   04.08
A       Total Units        0         0          0          0
        Total Revenue      0         0          0          0
B       Total Units        0         0          0          0
        Total Revenue      0         0          0          0 
C       Total Units        0         0          0          0
        Total Revenue      0         0          0          0
D       Total Units        0         0          0          0
        Total Revenue      0         0          0          0
     Total Overall Units   0         0          0          0
     Total Overall Revenue 0         0          0          0
</code></pre>

<ol>
<li>Can I add the dataframe to the dict by 'department name'?</li>
<li>Ultimate goal is to merge these two data objects into one unified data object or to literally align the objects for readability.</li>
</ol>

<p>Any ideas?  </p>

<p>To print in the desired order, you need to transpose the rows &amp; columns in the dates dictionary.   It is probably easiest to total the rows while doing that.  This makes the second object you mentioned unnecessary.  Except for the formatting, something like this should work:</p>

<pre><code>for dept, dates in df.items():

    # Transpose the rows and columns into two new dictionaries
    # called units and revenue.  At the same time, total the
    # units and revenue into two new "zztotal" entries.

    units = { "zztotal" : {}}
    revenues = { "zztotal" : {}}
    for date, products in dates.items():
        for product, stats in products.items():
            name = stats["name"]

            if not name in units:
                units[name] = {}
                revenues[name] = {}
            units[name][date] = stats["units"]
            revenue[name][date] = stats["revenue"]

            if not date in units["zztotal"]:
                units["zztotal"][date] = 0
                revenue["zztotal"][date] = 0
            units["zzotal"][date] += stats["units"]
            revenue["zzotal"][date] += stats["revenue"}

    # At this point we are ready to print the transposed
    # dictionaries.  Work is needed to line up the columns
    # so the printout is attractive.

    print dept
    print sorted(dates.keys())
    for name, dates in sorted(units.items()):
        if name != "zztotal":
            print name, "Total Units", [
                units[date] for date in sorted(dates)]
            print "Total Revenue", [
                revenue[date] for date in sorted(dates)]
        else:
            print "Total Overall Units", [
                units[date] for date in sorted(dates)]
            print "Total Overall Revenue", [
                revenue[date] for date in sorted(dates)]
</code></pre>

python pandas dictionary