1
{
  "type_a": {
    "2015-08-07": {
      "is_available": false, 
      "variable": 0.282
    }, 
    "2015-08-23": {
      "is_available": false, 
      "variable": 0.296
    },
    "2017-03-28": {
      "is_available": false, 
      "variable": 0.524
    }
  }, 
  "type_b": {
    "2015-06-27": {
      "is_available": true, 
      "variable": 0.038
    }, 
    "2015-07-30": {
      "is_available": true, 
      "variable": 0.035
    },
    "2017-04-27": {
      "is_available": true, 
      "variable": 0.158
    }
  }
}

I would like to load the above json into a pandas dataframe and make both is_available and variable column names, so eventual structure looks like so:

             data_source      is_available     variable
2015-08-07     type_a            false           0.282
2015-08-23     type_a            false           0.296
2017-03-28     type_a            false           0.524
2015-06-27     type_b            true            0.038  
2015-07-30     type_b            true            0.035
2015-04-27     type_b            true            0.158

currently, I do this:

pd.read_json(json)
1
  • 1
    Does it not break on the use of true and false? Because that's not python syntax Commented May 5, 2017 at 14:49

1 Answer 1

2

If I assume your data is called data, this will do it:

import pandas as pd

df = pd.DataFrame.from_dict({(i, j): data[i][j] for i in data.keys() for j in data[i].keys()}, orient='index')
df['data_source'] = df.index.droplevel(level=1)
df.index = df.index.droplevel(level=0)
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.