NorthAfrican
NorthAfrican

Reputation: 135

How to rename keys in a dictionary and make a dataframe of it?

I have a complex situation which I hope to solve and which might profit us all. I collected data from my API, added a pagination and inserted the complete data package in a tuple named q1 and finally I have made a dictionary named dict_1of that tuple which looks like this:

dict_1 = {100: {'ID': 100, 'DKSTGFase': None, 'DK': False, 'KM': None, 
'Country: {'Name': GE', 'City': {'Name': 'Berlin'}}, 
'Type': {'Name': '219'}, 'DKObject': {'Name': '8555', 'Object': {'Name': 'Car'}}, 
'Order': {'OrderId': 101, 'CreatedOn': '2018-07-06T16:54:36.783+02:00', 
'ModifiedOn': '2018-07-06T16:54:36.783+02:00', 
'Name': Audi, 'Client': {‘1’ }}, 'DKComponent': {'Name': ‘John’}}, 

{200: {'ID': 200, 'DKSTGFase': None, 'DK': False, ' KM ': None, 
'Country: {'Name': ES', 'City': {'Name': 'Madrid'}}, 'Type': {'Name': '220'}, 
'DKObject': {'Name': '8556', 'Object': {'Name': 'Car'}}, 
'Order': {'OrderId': 102, 'CreatedOn': '2018-07-06T16:54:36.783+02:00', 
'ModifiedOn': '2018-07-06T16:54:36.783+02:00', 
'Name': Mercedes, 'Client': {‘2’ }}, 'DKComponent': {'Name': ‘Sergio’}},

Please note that in the above dictionary I have just stated 2 records. The actual dictionary has 1400 records till it reaches ID 1500.

Now I want to 2 things:

  1. I want to change some keys for all the records. key DK has to become DK1. Key Name in Country has to become Name1 and Name in Object has to become 'Name2'
  2. The second thing I want is to make a dataFrame of the whole bunch of data. My expected outcome is:

enter image description here

This is my code:

q1 = response_2.json()
next_link = q1['@odata.nextLink']

q1 = [tuple(q1.values())]

while next_link:
    new_response = requests.get(next_link, headers=headers,  proxies=proxies)
    new_data = new_response.json()
    q1.append(tuple(new_data.values()))
    next_link = new_data.get('@odata.nextLink', None) 

dict_1 = {
  record['ID']: record
  for tup in q1
  for record in tup[2]
}

#print(dict_1)

for x in dict_1.values():
     x['DK1'] = x['DK']
     x['Country']['Name1'] = x['Country']['Name'] 
     x['Object']['Name2'] = x['Object']['Name'] 
    
df = pd.DataFrame(dict_1)

When i run this I receive the following Error:

Traceback (most recent call last):

  File "c:\data\FF\Desktop\Python\PythongMySQL\Talky.py", line 57, in <module>
    x['Country']['Name1'] = x['Country']['Name'] 

TypeError: 'NoneType' object is not subscriptable

Upvotes: 0

Views: 451

Answers (1)

Sayan Dey
Sayan Dey

Reputation: 856

working code

lists=[]
alldict=[{100: {'ID': 100, 'DKSTGFase': None, 'DK': False, 'KM': None, 
'Country': {'Name': 'GE', 'City': {'Name': 'Berlin'}}, 
'Type': {'Name': '219'}, 'DKObject': {'Name': '8555', 'Object': {'Name': 'Car'}}, 
'Order': {'OrderId': 101, 'CreatedOn': '2018-07-06T16:54:36.783+02:00', 
'ModifiedOn': '2018-07-06T16:54:36.783+02:00', 
'Name': 'Audi', 'Client': {'1' }}, 'DKComponent': {'Name': 'John'}}}]
for eachdict in alldict:
    key=list(eachdict.keys())[0]
    eachdict[key]['DK1']=eachdict[key]['DK']
    del eachdict[key]['DK']

    eachdict[key]['Country']['Name1']=eachdict[key]['Country']['Name']
    del eachdict[key]['Country']['Name']

    eachdict[key]['DKObject']['Object']['Name2']=eachdict[key]['DKObject']['Object']['Name']
    del eachdict[key]['DKObject']['Object']['Name']
    
    lists.append([key, eachdict[key]['DK1'], eachdict[key]['KM'], eachdict[key]['Country']['Name1'], 
    eachdict[key]['Country']['City']['Name'], eachdict[key]['DKObject']['Object']['Name2'], eachdict[key]['Order']['Client']])
pd.DataFrame(lists, columns=[<columnNamesHere>])

Output:

{100: {'ID': 100,
  'DKSTGFase': None,
  'KM': None,
  'Country': {'City': {'Name': 'Berlin'}, 'Name1': 'GE'},
  'Type': {'Name': '219'},
  'DKObject': {'Name': '8555', 'Object': {'Name2': 'Car'}},
  'Order': {'OrderId': 101,
   'CreatedOn': '2018-07-06T16:54:36.783+02:00',
   'ModifiedOn': '2018-07-06T16:54:36.783+02:00',
   'Name': 'Audi',
   'Client': {'1'}},
  'DKComponent': {'Name': 'John'},
  'DK1': False}}

Upvotes: 1

Related Questions