如何将 JSON 转换为数据框
How to convert JSON into dataframe
关于如何将此 JSON 文件转换为可用数据帧格式的任何想法:
pd.read_json("http://api.census.gov/data/2014/acsse/variables.json")
table 应该是这样的:http://api.census.gov/data/2014/acsse/variables.html
假设你从
开始
df = pd.read_json("http://api.census.gov/data/2014/acsse/variables.json")
问题是该列是字典:
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')
关于如何将此 JSON 文件转换为可用数据帧格式的任何想法:
pd.read_json("http://api.census.gov/data/2014/acsse/variables.json")
table 应该是这样的:http://api.census.gov/data/2014/acsse/variables.html
假设你从
开始df = pd.read_json("http://api.census.gov/data/2014/acsse/variables.json")
问题是该列是字典:
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')