简体   繁体   中英

'NoneType' object is not subscriptable within OrderedDict - pandas dataframe

I'm trying to pull out information from an ordered dictionary into a pandas dataframe. The ordered dict is from a query into a database. In order to upload information back into the database and manipulate it, I need it to be in a pandas dataframe format.

I have been using the following method to turn the ordered dict into a pd.DataFrame :

Ordered Dict Example:

x = [OrderedDict([('attributes',
               OrderedDict([('type', 'User'),
                            ('url',
                             '/services/data/v38.0/sobjects/User/0051300000C2dczAAB')])),
              ('Id', '0051300000C2dczAAB'),
              ('UserRole',
               OrderedDict([('attributes',
                             OrderedDict([('type', 'UserRole'),
                                          ('url',
                                           '/services/data/v38.0/sobjects/UserRole/00E1B000002DT6bUAG')])),
                            ('Name', 'Platform NA')]))]),
 OrderedDict([('attributes',
               OrderedDict([('type', 'User'),
                            ('url',
                             '/services/data/v34.0/sobjects/User/005a0000007oQYSST2')])),
              ('Id', '005a0000007oQYSST2'),
              ('UserRole', None)])]



df = pd.DataFrame(
           dict(Id = rec['Id'],
                UserRole = rec['UserRole']['Name']) for rec in x)

This has been working great, except when I have a record where there is no underlying record for (in this example) UserRole . I get the error 'NoneType' object is not subscriptable , since I am trying to extract ['Name'] from the OrderedDict that is x['UserRole'] . I've tried creating another generator to pull this out, or a for loop without success. This example has two features, my real dataset is 10+ features, and some, not all of them have a None record in there.

Any help is much appreciated!

You can have a helper function.

def helper(x, attribute):
    return None if x is None else x[attribute]

df = pd.DataFrame(
           dict(Id = rec['Id'],
                UserRole = helper(rec['UserRole'], "Name")) for rec in x)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM