1

I have a df as follows:

                dates  values
0 2020-01-01 00:15:00    25.7
1 2020-01-01 00:30:00    25.0
2 2020-01-01 00:45:00    24.6
3 2020-01-01 01:00:00    24.6
4 2020-01-01 01:15:00    25.0
5 2020-01-01 01:30:00    25.6
6 2020-01-01 01:45:00    26.2
7 2020-01-01 02:00:00    26.5
8 2020-01-01 02:15:00    26.3
9 2020-01-01 02:30:00    25.7

and I do:

df = df.to_json(orient='records', date_unit='s')
print({'items' : df})

It gives me output as follows:

{
    "items": "[{\"dates\":1577837700,\"values\":25.7},{\"dates\":1577838600,\"values\":25.0},{\"dates\":1577839500,\"values\":24.6},{\"dates\":1577840400,\"values\":24.6},{\"dates\":1577841300,\"values\":25.0},{\"dates\":1577842200,\"values\":25.6},{\"dates\":1577843100,\"values\":26.2},{\"dates\":1577844000,\"values\":26.5},{\"dates\":1577844900,\"values\":26.3},{\"dates\":1577845800,\"values\":25.7}]" }

I want the output to look like

{
    "items": [[1577837700, 25.7],[1577838600,25.0],[1577839500,24.6],[1577840400,24.6],[1577841300,25.0],[1577842200,25.6],[1577843100,26.2],[1577844000,26.5],[1577844900,26.3],[1577845800,25.7]] }

That is, from the output that I get from my code, I want to have all the records in the form of a list instead of a dict

Is there a way I can do it?

1 Answer 1

2

Try orient="values":

df.to_json(orient='values', date_unit='s')    

'[[1577837700,25.7],[1577838600,25.0],[1577839500,24.6],[1577840400,24.6],[1577841300,25.0],[1577842200,25.6],[1577843100,26.2],[1577844000,26.5],[1577844900,26.3],[1577845800,25.7]]'}

This just dumps out the .values (or .to_numpy) output as json. See DataFrame.to_json for more.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.