Reputation: 2592
I have the following code:
responsedata = requests.get(url, data=data, headers=hed, verify=False)
sample_object = pd.DataFrame(responsedata.json())['results'].to_dict()
func(sample_object)
Now, I have another get call using function which does some manipulation over the data:
responsedata2 = get_data(url2)
I do know that:
responsedata2
is equivalent to responsedata.json()['results']
as if I do:
print responsedata2
print responsedata.json()['results']
I will get the same output (If called with the same URL).
My question is how can I create sample_object2
that will be equivalent to sample_object1
so I will be able to do: func(sample_object2)
Any idea how to make it works? It seems simply but the involvement of the DataFrame
makes it hard.
Edit:
To explain better what I'm after.
I want to write a function that gets object like responsedata2
and return object like sample_object
.
Upvotes: 0
Views: 52
Reputation: 61
From what I can tell pd.DataFrame(responsedata.json())['results'].to_dict()
gives you {0: (first data set), 1: (second data set)}
,
while responsedata.json()['results']
gives you a list of [(first data set), (second data set)]
.
In order to turn the list into a dictionary like the first, use
sample_object = {i: data for i, data in enumerate(responsedata2)}
enumerate
is a generator that takes an iterable such as ['a', 'b', 'c', ...] and returns tuples (0, 'a'), (1, 'b'), etc.
Upvotes: 1