我从软件 API 检索数据(JSON 格式)并将其转换为数据帧以将其写入 CSV(pandas 库)。我会添加一列时间。我希望在第一行写“时间”,例如在以下行写“Fri Mar 29 09:16:02 2019”。关于如何实现这一目标的想法?
我必须添加时间,但只是在数据框的第一行。
import json
import pandas as pd
import time
import urllib.request
url='http://localhost:47800/api/v1/bacnet/devices/0/objects?properties=present-value&properties=object-name'
req = urllib.request.Request(url)
r = urllib.request.urlopen(req).read()
data = json.loads(r.decode('utf-8'))
time=time.asctime(time.localtime(time.time()))
result = pd.io.json.json_normalize(data['objects'])
result_tri = result.reindex(columns=[time,'object-name','present-value'])
当前结果
Fri Mar 29 09:47:36 2019 object-name present-value
0 NaN Température_1 0 660.0
1 NaN Humidité_1 1 497.0
2 NaN Pression_1 2 497.0
3 NaN Vitesse_Vent 3 497.0
4 NaN Luminosité 4 497.0
5 NaN Etat_Pompe 3 0.0
6 NaN Greisch_Simulator NaN
7 NaN networkPort 30800 NaN
想要的结果
Time object-name present-value
0 Fri Mar 29 09:47:36 2019 Température_1 0 660.0
1 Fri Mar 29 09:47:36 2019 Humidité_1 1 497.0
2 Fri Mar 29 09:47:36 2019 Pression_1 2 497.0
3 Fri Mar 29 09:47:36 2019 Vitesse_Vent 3 497.0
4 Fri Mar 29 09:47:36 2019 Luminosité 4 497.0
5 Fri Mar 29 09:47:36 2019 Etat_Pompe 3 0.0
6 Fri Mar 29 09:47:36 2019 Greisch_Simulator NaN
7 Fri Mar 29 09:47:36 2019 networkPort 30800 NaN
眼眸繁星
慕妹3146593
相关分类