3
\$\begingroup\$

I request data from a table in a database and each line comes as a dictionary like this one :

{
 "timestamp" : 1234657890,
 "prices" : {
 "AAA" : 111,
 "BBB" : 222,
 ...
 "ZZZ" : 999
 }
}

From all those lines i wanted to create a dataframe like this:

Timestamp AAA BBB ... ZZZ
1234657890 111 222 ... 999
1234567891 110 223 ... 997
 ...
1324657899 123 208 ... 1024

So i did :

rawData = database_request()
listPrices = []
for row in rawData
 tmp = {'timestamp': row['timestamp']}
 tmp.update({name : price for name,price in row['prices'].items()})
 listPrices.append(tmp)
df = pd.DataFrame(listePrices)

So i was wondering if there were a more pythonic way to do this ?

hjpotter92
8,9111 gold badge26 silver badges49 bronze badges
asked Oct 14, 2020 at 9:00
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Does it only have prices and timestamps in the dictionary? \$\endgroup\$ Commented Oct 14, 2020 at 9:40
  • \$\begingroup\$ yes only those two fields \$\endgroup\$ Commented Oct 14, 2020 at 9:42
  • 1
    \$\begingroup\$ You need pd.json_normalize here. \$\endgroup\$ Commented Oct 14, 2020 at 12:05

1 Answer 1

3
\$\begingroup\$

Your rawData (which should be ideally named raw_data, python suggests a style guide to name variables and functions in lower_snake_case) is already in a list structure. You can manipulate this in place, without having to process the whole dataset manually.

for row in raw_data:
 row.update(row.pop("prices"))
answered Oct 14, 2020 at 10:43
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.