Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Json format ? #26

Closed
slamer59 opened this issue Mar 24, 2017 · 2 comments
Closed

Change Json format ? #26

slamer59 opened this issue Mar 24, 2017 · 2 comments

Comments

@slamer59
Copy link

slamer59 commented Mar 24, 2017

Hello,
I need to construct my json like :

{
  "x": [
    5.0,
    3.0,
    8.0,
    5.0]
  ], 
  "y": [
    7.0,
    2.0,
    6.0,
    4.0]
}

I have this, which is exactly what to_json gives:
[{"x":5,"y":7},{"x":3,"y":2},{"x":8,"y":6},{"x":5,"y":4}]

Is it possible to change this behavior ?
It might be a pandas question.

Thanks for your efforts ! I understand better how REST works now :)
Thomas

@sheppard
Copy link
Member

You can change the orientation using the orient URL parameter which is passed on to DataFrame.to_json(). I don't know if there is an orient that does exactly what you want but columns or values might do the trick.

@slamer59
Copy link
Author

Hello,
I find a solution for this !
data_frame = pd.DataFrame.from_dict(data, orient='index')

I have to play with orientation of data as you suggested. Going from 'index' to 'columns' do the trick.

Thanks for your suggestion and this app. It helps me a lot to understand django rest :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants