Dataframe из словаря со списком в качестве последнего значения - PullRequest
1 голос
/ 10 июля 2020

У меня есть словарь для представления сектора / отраслевой группы / отрасли / подотрасли (последний элемент - список)

{
 'Energy': {'Energy': {'Energy Equipment & Services': ['Oil & Gas Drilling',
                                                       'Oil & Gas Equipment & '
                                                       'Services'],
                       'Oil, Gas & Consumable Fuels': ['Integrated Oil & Gas',
                                                       'Oil & Gas Exploration '
                                                       '& Production',
                                                       'Oil & Gas Refining & '
                                                       'Marketing',
                                                       'Oil & Gas Storage & '
                                                       'Transportation',
                                                       'Coal & Consumable '
                                                       'Fuels']}},
 'Financials': {'Banks': {'Banks': ['Diversified Banks', 'Regional Banks'],
                          'Thrifts & Mortgage Finance': ['Thrifts & Mortgage '
                                                         'Finance']},
                'Diversified Financials': {'Capital Markets': ['Asset '
                                                               'Management & '
                                                               'Custody Banks',
                                                               'Investment '
                                                               'Banking & '
                                                               'Brokerage',
                                                               'Diversified '
                                                               'Capital '
                                                               'Markets',
                                                               'Financial '
                                                               'Exchanges & '
                                                               'Data'],
                                           'Consumer Finance': ['Consumer '
                                                                'Finance'],
                                           'Diversified Financial Services': ['Other '
                                                                              'Diversified '
                                                                              'Financial '
                                                                              'Services',
                                                                              'Multi-Sector '
                                                                              'Holdings',
                                                                              'Specialized '
                                                                              'Finance'],
                                           'Mortgage Real Estate Investment Trusts (REITs)': ['Mortgage '
                                                                                              'REITs']},
                'Insurance': {'Insurance': ['Insurance Brokers',
                                            'Life & Health Insurance',
                                            'Multi-line Insurance',
                                            'Property & Casualty Insurance',
                                            'Reinsurance']}},
 'Health Care': {'Health Care Equipment & Services': {'Health Care Equipment & Supplies': ['Health '
                                                                                           'Care '
                                                                                           'Equipment',
                                                                                           'Health '
                                                                                           'Care '
                                                                                           'Supplies'],
                                                      'Health Care Providers & Services': ['Health '
                                                                                           'Care '
                                                                                           'Distributors',
                                                                                           'Health '
                                                                                           'Care '
                                                                                           'Services',
                                                                                           'Health '
                                                                                           'Care '
                                                                                           'Facilities',
                                                                                           'Managed '
                                                                                           'Health '
                                                                                           'Care'],
                                                      'Health Care Technology': ['Health '
                                                                                 'Care '
                                                                                 'Technology']},
                 'Pharmaceuticals, Biotechnology  & Life Sciences': {'Biotechnology': ['Biotechnology'],
                                                                     'Life Sciences Tools & Services': ['Life '
                                                                                                        'Sciences '
                                                                                                        'Tools '
                                                                                                        '& '
                                                                                                        'Services'],
                                                                     'Pharmaceuticals': ['Pharmaceuticals']}},
 'Industrials': {'Capital Goods': {'Aerospace & Defense': ['Aerospace & '
                                                           'Defense'],
                                   'Building Products': ['Building Products'],
                                   'Construction & Engineering': ['Construction '
                                                                  '& '
                                                                  'Engineering'],
                                   'Electrical Equipment': ['Electrical '
                                                            'Components & '
                                                            'Equipment',
                                                            'Heavy Electrical '
                                                            'Equipment'],
                                   'Industrial Conglomerates': ['Industrial '
                                                                'Conglomerates'],
                                   'Machinery': ['Construction Machinery & '
                                                 'Heavy Trucks',
                                                 'Agricultural & Farm '
                                                 'Machinery',
                                                 'Industrial Machinery'],
                                   'Trading Companies & Distributors': ['Trading '
                                                                        'Companies '
                                                                        '& '
                                                                        'Distributors']},
                 'Commercial & Professional Services': {'Commercial Services & Supplies': ['Commercial '
                                                                                           'Printing',
                                                                                           'Environmental '
                                                                                           '& '
                                                                                           'Facilities '
                                                                                           'Services',
                                                                                           'Office '
                                                                                           'Services '
                                                                                           '& '
                                                                                           'Supplies',
                                                                                           'Diversified '
                                                                                           'Support '
                                                                                           'Services',
                                                                                           'Security '
                                                                                           '& '
                                                                                           'Alarm '
                                                                                           'Services'],
                                                        'Professional Services': ['Human '
                                                                                  'Resource '
                                                                                  '& '
                                                                                  'Employment '
                                                                                  'Services',
                                                                                  'Research '
                                                                                  '& '
                                                                                  'Consulting '
                                                                                  'Services']},
                 'Transportation': {'Air Freight & Logistics': ['Air Freight & '
                                                                'Logistics'],
                                    'Airlines': ['Airlines'],
                                    'Marine': ['Marine'],
                                    'Road & Rail': ['Railroads', 'Trucking'],
                                    'Transportation Infrastructure': ['Airport '
                                                                      'Services',
                                                                      'Highways '
                                                                      '& '
                                                                      'Railtracks',
                                                                      'Marine '
                                                                      'Ports & '
                                                                      'Services']}},
 'Information Technology': {'Semiconductors & Semiconductor Equipment': {'Semiconductors & Semiconductor Equipment': ['Semiconductor '
                                                                                                                      'Equipment',
                                                                                                                      'Semiconductors']},
                            'Software & Services': {'IT Services': ['IT '
                                                                    'Consulting '
                                                                    '& Other '
                                                                    'Services',
                                                                    'Data '
                                                                    'Processing '
                                                                    '& '
                                                                    'Outsourced '
                                                                    'Services',
                                                                    'Internet '
                                                                    'Services '
                                                                    '& '
                                                                    'Infrastructure'],
                                                    'Software': ['Application '
                                                                 'Software',
                                                                 'Systems '
                                                                 'Software']},
                            'Technology Hardware & Equipment': {'Communications Equipment': ['Communications '
                                                                                             'Equipment'],
                                                                'Electronic Equipment, Instruments & Components': ['Electronic '
                                                                                                                   'Equipment '
                                                                                                                   '& '
                                                                                                                   'Instruments',
                                                                                                                   'Electronic '
                                                                                                                   'Components',
                                                                                                                   'Electronic '
                                                                                                                   'Manufacturing '
                                                                                                                   'Services',
                                                                                                                   'Technology '
                                                                                                                   'Distributors'],
                                                                'Technology Hardware, Storage & Peripherals': ['Technology '
                                                                                                               'Hardware, '
                                                                                                               'Storage '
                                                                                                               '& '
                                                                                                               'Peripherals']}},
 'Materials': {'Materials': {'Chemicals': ['Commodity Chemicals',
                                           'Diversified Chemicals',
                                           'Fertilizers & Agricultural '
                                           'Chemicals',
                                           'Industrial Gases',
                                           'Specialty Chemicals'],
                             'Construction Materials': ['Construction '
                                                        'Materials'],
                             'Containers & Packaging': ['Metal & Glass '
                                                        'Containers',
                                                        'Paper Packaging'],
                             'Metals & Mining': ['Aluminum',
                                                 'Diversified Metals & Mining',
                                                 'Copper',
                                                 'Gold',
                                                 'Precious Metals & Minerals',
                                                 'Silver',
                                                 'Steel'],
                             'Paper & Forest Products': ['Forest Products',
                                                         'Paper Products']}},
 'Real Estate': {'Real Estate': {'Equity Real Estate Investment Trusts (REITs)': ['Diversified '
                                                                                  'REITs',
                                                                                  'Industrial '
                                                                                  'REITs',
                                                                                  'Hotel '
                                                                                  '& '
                                                                                  'Resort '
                                                                                  'REITs',
                                                                                  'Office '
                                                                                  'REITs',
                                                                                  'Health '
                                                                                  'Care '
                                                                                  'REITs',
                                                                                  'Residential '
                                                                                  'REITs',
                                                                                  'Retail '
                                                                                  'REITs',
                                                                                  'Specialized '
                                                                                  'REITs'],
                                 'Real Estate Management & Development': ['Diversified '
                                                                          'Real '
                                                                          'Estate '
                                                                          'Activities',
                                                                          'Real '
                                                                          'Estate '
                                                                          'Operating '
                                                                          'Companies',
                                                                          'Real '
                                                                          'Estate '
                                                                          'Development',
                                                                          'Real '
                                                                          'Estate '
                                                                          'Services']}},
 'Utilities': {'Utilities': {'Electric Utilities': ['Electric Utilities'],
                             'Gas Utilities': ['Gas Utilities'],
                             'Independent Power and Renewable Electricity Producers': ['Independent '
                                                                                       'Power '
                                                                                       'Producers '
                                                                                       '& '
                                                                                       'Energy '
                                                                                       'Traders',
                                                                                       'Renewable '
                                                                                       'Electricity'],
                             'Multi-Utilities': ['Multi-Utilities'],
                             'Water Utilities': ['Water Utilities']}}}

Прямо сейчас я могу превратить его во фрейм данных следующим образом:

    df = pd.DataFrame([(i, j, k, l)
                       for i in cleaned_gics_dict.keys()
                       for j in cleaned_gics_dict[i].keys()
                       for k in cleaned_gics_dict[i][j].keys()
                       for l in cleaned_gics_dict[i][j][k]])

enter image description here

Or

    df = pd.DataFrame.from_dict({(i, j, k): pd.Series(l)
                                for i in cleaned_gics_dict.keys()
                                for j in cleaned_gics_dict[i].keys()
                                for k, l in cleaned_gics_dict[i][j].items()},
                                orient='index')

This would output the format I'd like, but each element of the list would take a column enter image description here

Is there a way to have each of the element of the list take a row? I can't wrap my head around it.

enter image description here

Those links may help:

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...