[英]Python reading JSON in dataframe
I have an SQL database which has two columns. 我有一个具有两列的SQL数据库。 One has the timestamp, the other holds data in JSON format
一个带有时间戳,另一个带有JSON格式的数据
for example df: 例如df:
ts data
'2017-12-18 02:30:20.553' {'name':'bob','age':10, 'location':{'town':'miami','state':'florida'}}
'2017-12-18 02:30:21.101' {'name':'dan','age':15, 'location':{'town':'new york','state':'new york'}}
'2017-12-18 02:30:21.202' {'name':'jay','age':11, 'location':{'town':'tampa','state':'florida'}}
If I do the following : 如果我执行以下操作:
df = df['data'][0]
print (df['name'].encode('ascii', 'ignore'))
I get : 我得到:
'bob'
Is there a way I can get all of the data correspondings to a JSON key for the whole column? 有没有办法让整个列的JSON键对应的所有数据?
(ie for the df column 'data' get 'name') (即对于df列“数据”获取“名称”)
'bob'
'dan'
'jay'
Essentially I would like to be able to make a new df column called 'name' 本质上,我希望能够创建一个名为'name'的新df列
You can use json_normalize
ie 您可以使用
json_normalize
即
pd.io.json.json_normalize(df['data'])['name']
0 bob
1 dan
2 jay
Name: name, dtype: object
IIUC, lets use apply
with lambda function to select value from dictionary by key: IIUC,让我们使用带有lambda函数的
apply
通过键从字典中选择值:
df['data'].apply(lambda x: x['name'])
Output: 输出:
0 bob
1 dan
2 jay
Name: data, dtype: object
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.