如何将JSON转换为数据帧

时间:2016-08-09 08:17:01

标签: python json pandas census

有关如何将此JSON文件转换为可用数据帧格式的任何想法:

pd.read_json("http://api.census.gov/data/2014/acsse/variables.json")

以下是表格的外观:http://api.census.gov/data/2014/acsse/variables.html

1 个答案:

答案 0 :(得分:3)

假设您从

开始
df = pd.read_json("http://api.census.gov/data/2014/acsse/variables.json")

问题是该列是dicts:

In [28]: df.variables.head()
Out[28]: 
AIANHH    {u'concept': u'Selectable Geographies', u'pred...
ANRC      {u'concept': u'Selectable Geographies', u'pred...
BST       {u'concept': u'Selectable Geographies', u'pred...
CBSA      {u'concept': u'Selectable Geographies', u'pred...
CD        {u'concept': u'Selectable Geographies', u'pred...
Name: variables, dtype: object

但您可以通过应用Series

来解决此问题
In [27]: df.variables.apply(pd.Series)
Out[27]: 
                                                         concept  \
AIANHH                                    Selectable Geographies   
ANRC                                      Selectable Geographies   
BST                                       Selectable Geographies   
CBSA                                      Selectable Geographies   
CD                                        Selectable Geographies   
CNECTA                                    Selectable Geographies   
...

这可能是您想要的DataFrame,可以通过以下方式显示:

In [32]: df.variables.apply(pd.Series).columns
Out[32]: Index([u'concept', u'label', u'predicateOnly', u'predicateType'], dtype='object')