Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Nested Dictionary to excel
#1
My dictionary will be similar to below. I want it to not have the terms in red and I want each row to be a quarter date as output and each title to be a column populated with the dictionary values. Any help of how to make this a table in excel? Thanks!!

example output:
Total change liabilities .................
2018-09-30 64000000 ................

{'cashflowStatementHistoryQuarterly': {'APA':
[{'2018-09-30': {'changeToLiabilities': 64000000, 'totalCashflowsFromInvestingActivities': -926000000, 'netBorrowings': -228000000, 'totalCashFromFinancingActivities': -459000000, 'changeToOperatingActivities': -48000000, 'netIncome': 81000000, 'changeInCash': -379000000, 'totalCashFromOperatingActivities': 1006000000, 'depreciation': 676000000, 'otherCashflowsFromInvestingActivities': -22000000, 'dividendsPaid': -96000000, 'changeToInventory': 2000000, 'changeToAccountReceivables': -12000000, 'otherCashflowsFromFinancingActivities': -135000000, 'changeToNetincome': 243000000, 'capitalExpenditures': -942000000}},

{'2018-06-30': {'changeToLiabilities': -20000000, 'totalCashflowsFromInvestingActivities': -1024000000, 'netBorrowings': -228000000, 'totalCashFromFinancingActivities': -194000000, 'changeToOperatingActivities': 213000000, 'netIncome': 195000000, 'changeInCash': -105000000, 'totalCashFromOperatingActivities': 1113000000, 'depreciation': 656000000, 'otherCashflowsFromInvestingActivities': -11000000, 'dividendsPaid': -96000000, 'changeToInventory': 24000000, 'changeToAccountReceivables': -36000000, 'otherCashflowsFromFinancingActivities': -98000000, 'changeToNetincome': 81000000, 'capitalExpenditures': -1017000000}},
{'2018-03-31': {'changeToLiabilities': 66000000, 'totalCashflowsFromInvestingActivities': -890000000, 'netBorrowings': -150000000, 'totalCashFromFinancingActivities': -316000000, 'changeToOperatingActivities': -152000000, 'netIncome': 145000000, 'changeInCash': -591000000, 'totalCashFromOperatingActivities': 615000000, 'depreciation': 596000000, 'otherCashflowsFromInvestingActivities': -22000000, 'dividendsPaid': -95000000, 'changeToInventory': -33000000, 'changeToAccountReceivables': -65000000, 'otherCashflowsFromFinancingActivities': -71000000, 'changeToNetincome': 58000000, 'capitalExpenditures': -877000000}},
{'2017-12-31': {'changeToLiabilities': 61000000, 'totalCashflowsFromInvestingActivities': -793000000, 'netBorrowings': -150000000, 'totalCashFromFinancingActivities': -149000000, 'changeToOperatingActivities': 32000000, 'netIncome': 456000000, 'changeInCash': -274000000, 'totalCashFromOperatingActivities': 668000000, 'depreciation': 632000000, 'otherCashflowsFromInvestingActivities': -45000000, 'dividendsPaid': -95000000, 'changeToInventory': 15000000, 'changeToAccountReceivables': -200000000, 'otherCashflowsFromFinancingActivities': -54000000, 'changeToNetincome': -328000000, 'capitalExpenditures': -763000000}}]}}
Reply
#2
Have you already created the dictionary?
If not, it's much easier to handle if the embedded dictionaries are not in a list
Example:
import json
import os
import pandas as pd


class DictToExcel:
    os.chdir(os.path.abspath(os.path.dirname(__file__)))
    def __init__(self):
        self.exdict = {
            'cashflowStatementHistoryQuarterly': {
                'APA': {
                    '2018-09-30': {
                        'changeToLiabilities': 64000000, 
                        'totalCashflowsFromInvestingActivities': -926000000, 
                        'netBorrowings': -228000000, 
                        'totalCashFromFinancingActivities': -459000000, 
                        'changeToOperatingActivities': -48000000, 
                        'netIncome': 81000000, 
                        'changeInCash': -379000000, 
                        'totalCashFromOperatingActivities': 1006000000, 
                        'depreciation': 676000000, 
                        'otherCashflowsFromInvestingActivities': -22000000, 
                        'dividendsPaid': -96000000, 
                        'changeToInventory': 2000000, 
                        'changeToAccountReceivables': -12000000, 
                        'otherCashflowsFromFinancingActivities': -135000000, 
                        'changeToNetincome': 243000000, 
                        'capitalExpenditures': -942000000
                    },
                    '2018-06-30': {
                        'changeToLiabilities': -20000000, 
                        'totalCashflowsFromInvestingActivities': -1024000000, 
                        'netBorrowings': -228000000, 
                        'totalCashFromFinancingActivities': -194000000, 
                        'changeToOperatingActivities': 213000000, 
                        'netIncome': 195000000, 
                        'changeInCash': -105000000, 
                        'totalCashFromOperatingActivities': 1113000000, 
                        'depreciation': 656000000, 
                        'otherCashflowsFromInvestingActivities': -11000000, 
                        'dividendsPaid': -96000000, 
                        'changeToInventory': 24000000, 
                        'changeToAccountReceivables': -36000000, 
                        'otherCashflowsFromFinancingActivities': -98000000, 
                        'changeToNetincome': 81000000, 
                        'capitalExpenditures': -1017000000
                    },
                    '2018-03-31': {
                        'changeToLiabilities': 66000000,
                        'totalCashflowsFromInvestingActivities': -890000000,
                        'netBorrowings': -150000000,
                        'totalCashFromFinancingActivities': -316000000,
                        'changeToOperatingActivities': -152000000,
                        'netIncome': 145000000,
                        'changeInCash': -591000000,
                        'totalCashFromOperatingActivities': 615000000,
                        'depreciation': 596000000,
                        'otherCashflowsFromInvestingActivities': -22000000,
                        'dividendsPaid': -95000000,
                        'changeToInventory': -33000000,
                        'changeToAccountReceivables': -65000000,
                        'otherCashflowsFromFinancingActivities': -71000000,
                        'changeToNetincome': 58000000,
                        'capitalExpenditures': -877000000
                    },
                    '2017-12-31': {
                        'changeToLiabilities': 61000000,
                        'totalCashflowsFromInvestingActivities': -793000000,
                        'netBorrowings': -150000000,
                        'totalCashFromFinancingActivities': -149000000,
                        'changeToOperatingActivities': 32000000,
                        'netIncome': 456000000,
                        'changeInCash': -274000000,
                        'totalCashFromOperatingActivities': 668000000,
                        'depreciation': 632000000,
                        'otherCashflowsFromInvestingActivities': -45000000,
                        'dividendsPaid': -95000000,
                        'changeToInventory': 15000000,
                        'changeToAccountReceivables': -200000000,
                        'otherCashflowsFromFinancingActivities': -54000000,
                        'changeToNetincome': -328000000,
                        'capitalExpenditures': -763000000
                    }
                }
            }
        }
        self.display_dict(self.exdict)

    def display_dict(self, thedict):
        for key, value in thedict.items():
            if isinstance(value, dict):
                print(f'\n{key}:')
                self.display_dict(value)
            else:
                print(f'    {key}: {value}')


if __name__ == '__main__':
    DictToExcel()
the display_dict method shows how easy it is to traverse the dictionary if in this format
and produces the following results:
Output:
cashflowStatementHistoryQuarterly: APA: 2018-09-30: changeToLiabilities: 64000000 totalCashflowsFromInvestingActivities: -926000000 netBorrowings: -228000000 totalCashFromFinancingActivities: -459000000 changeToOperatingActivities: -48000000 netIncome: 81000000 changeInCash: -379000000 totalCashFromOperatingActivities: 1006000000 depreciation: 676000000 otherCashflowsFromInvestingActivities: -22000000 dividendsPaid: -96000000 changeToInventory: 2000000 changeToAccountReceivables: -12000000 otherCashflowsFromFinancingActivities: -135000000 changeToNetincome: 243000000 capitalExpenditures: -942000000 2018-06-30: changeToLiabilities: -20000000 totalCashflowsFromInvestingActivities: -1024000000 netBorrowings: -228000000 totalCashFromFinancingActivities: -194000000 changeToOperatingActivities: 213000000 netIncome: 195000000 changeInCash: -105000000 totalCashFromOperatingActivities: 1113000000 depreciation: 656000000 otherCashflowsFromInvestingActivities: -11000000 dividendsPaid: -96000000 changeToInventory: 24000000 changeToAccountReceivables: -36000000 otherCashflowsFromFinancingActivities: -98000000 changeToNetincome: 81000000 capitalExpenditures: -1017000000 2018-03-31: changeToLiabilities: 66000000 totalCashflowsFromInvestingActivities: -890000000 netBorrowings: -150000000 totalCashFromFinancingActivities: -316000000 changeToOperatingActivities: -152000000 netIncome: 145000000 changeInCash: -591000000 totalCashFromOperatingActivities: 615000000 depreciation: 596000000 otherCashflowsFromInvestingActivities: -22000000 dividendsPaid: -95000000 changeToInventory: -33000000 changeToAccountReceivables: -65000000 otherCashflowsFromFinancingActivities: -71000000 changeToNetincome: 58000000 capitalExpenditures: -877000000 2017-12-31: changeToLiabilities: 61000000 totalCashflowsFromInvestingActivities: -793000000 netBorrowings: -150000000 totalCashFromFinancingActivities: -149000000 changeToOperatingActivities: 32000000 netIncome: 456000000 changeInCash: -274000000 totalCashFromOperatingActivities: 668000000 depreciation: 632000000 otherCashflowsFromInvestingActivities: -45000000 dividendsPaid: -95000000 changeToInventory: 15000000 changeToAccountReceivables: -200000000 otherCashflowsFromFinancingActivities: -54000000 changeToNetincome: -328000000 capitalExpenditures: -763000000
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  need to compare 2 values in a nested dictionary jss 2 855 Nov-30-2023, 03:17 PM
Last Post: Pedroski55
  Nested dictionary acting strange Pedroski55 2 2,085 May-13-2021, 10:37 PM
Last Post: Pedroski55
  format the output from a nested dictionary. nostradamus64 9 4,526 May-03-2021, 04:45 PM
Last Post: nostradamus64
Lightbulb Python Nested Dictionary michaelserra 2 2,594 Apr-18-2021, 07:54 AM
Last Post: michaelserra
  nested dictionary rkpython 7 3,254 May-29-2020, 11:13 AM
Last Post: rkpython
  Nested Dictionary/List tonybrown3 5 3,143 May-08-2020, 01:27 AM
Last Post: tonybrown3
  Help: for loop with dictionary and nested lists mart79 1 1,863 Apr-12-2020, 02:52 PM
Last Post: TomToad
  Transforming nested key-tuples into their dictionary values ClassicalSoul 4 2,657 Apr-11-2020, 04:36 PM
Last Post: bowlofred
  [split] Print JSON Dictionary to Excel? venukommu 1 2,291 Nov-15-2019, 09:33 PM
Last Post: micseydel
  Exporting list with dictionary to Excel veromi22 0 3,042 Oct-15-2019, 12:54 AM
Last Post: veromi22

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020