Home > front end >  How to split large nested set into separate sets?
How to split large nested set into separate sets?

Time:10-19

Hi I have a set of data which I extracted from an api and I am trying to split the data in the set down into separate sets since currently they are all nested in the larger set.

My current set:

api = {
  "9/30/2018": {
    "Capital Expenditure": "-13313000", 
    "End Cash Position": "25913000", 
    "Financing Cash Flow": "-87876000", 
    "Free Cash Flow": "64121000", 
    "Income Tax Paid Supplemental Data": "10417000", 
    "Interest Paid Supplemental Data": "3022000", 
    "Investing Cash Flow": "16066000", 
    "Issuance of Capital Stock": "669000", 
    "Issuance of Debt": "6969000", 
    "Operating Cash Flow": "77434000", 
    "Repayment of Debt": "-6500000", 
    "Repurchase of Capital Stock": "-72738000"
  }, 
  "9/30/2019": {
    "Capital Expenditure": "-10495000", 
    "End Cash Position": "50224000", 
    "Financing Cash Flow": "-90976000", 
    "Free Cash Flow": "58896000", 
    "Income Tax Paid Supplemental Data": "15263000", 
    "Interest Paid Supplemental Data": "3423000", 
    "Investing Cash Flow": "45896000", 
    "Issuance of Capital Stock": "781000", 
    "Issuance of Debt": "6963000", 
    "Operating Cash Flow": "69391000", 
    "Repayment of Debt": "-8805000", 
    "Repurchase of Capital Stock": "-66897000"
  }, 
  "9/30/2020": {
    "Capital Expenditure": "-7309000", 
    "End Cash Position": "39789000", 
    "Financing Cash Flow": "-86820000", 
    "Free Cash Flow": "73365000", 
    "Income Tax Paid Supplemental Data": "9501000", 
    "Interest Paid Supplemental Data": "3002000", 
    "Investing Cash Flow": "-4289000", 
    "Issuance of Capital Stock": "880000", 
    "Issuance of Debt": "16091000", 
    "Operating Cash Flow": "80674000", 
    "Repayment of Debt": "-12629000", 
    "Repurchase of Capital Stock": "-72358000"
  }, 
  "ttm": {
    "Capital Expenditure": "-9646000", 
    "End Cash Position": "35276000", 
    "Financing Cash Flow": "-94328000", 
    "Free Cash Flow": "94768000", 
    "Income Tax Paid Supplemental Data": "19627000", 
    "Interest Paid Supplemental Data": "2597000", 
    "Investing Cash Flow": "-9849000", 
    "Issuance of Capital Stock": "1011000", 
    "Issuance of Debt": "22370000", 
    "Operating Cash Flow": "104414000", 
    "Repayment of Debt": "-7500000", 
    "Repurchase of Capital Stock": "-83410000"
  }
}

My desired outcome would be: s_19_30_2018 = ["Capital Expenditure": "-13313000"...]

s_19_30_2019 = ["Capital Expenditure": "-10495000"...]

s_19_30_2020 = ["Capital Expenditure": "-7309000"...]

s_ttm = ["Capital Expenditure": "-9646000"...]

This is so that I can access the data with more ease and add them to a sql database.

I have tried by doing s_19_30_2018 = api['19/30/2018'] but I keep getting 'type error string indices must be integers '.

Any help would be appreciated in python >.<

CodePudding user response:

Given this structure:

api = {
  "9/30/2018": {
    "Capital Expenditure": "-13313000", 
    "End Cash Position": "25913000", 

  }, 
  "9/30/2019": {
  ....

to get a list of key,values for the first entry can run:

for key,value in api["9/30/2018"]:
  l = [key, value]
  print(f" {key}, {value}")
  # prints "Capital Expenditure -133313000"

to go through all the items

#get the keys
ks = api.keys()
for k in ks:
  for key, value, in api[k]:
    print(f" {key}, {value}")
      # prints "Capital Expenditure -133313000"] ...

CodePudding user response:

Your dictionary keys are string so use quotes when you access them like

s_19_30_2018 = api["9/30/2018"]

also I don't see any key such as "19_30_2018" in your dictionary.

CodePudding user response:

ks = api.keys()
for k in ks:
  for key, value, in api[k]:
    print(f" {key}, {value}")

By using code you can solve your problem.

  • Related