下面编写的代码旨在从给定的URL中提取JSON数据。(我已删除了密钥)。然后,我将其格式化并将其更改为CSV。当运行并打印该程序时,我看到已提取的第一行,但是我打算附加的后续行仅添加到第一行(df),而在下一行不进行编译。有没有简单的方法可以实现?这样,每次完成time.sleep()周期后,该行就会添加到上一个睡眠周期,而不是原始df。
df = pd.read_json('https://forex.1forge.com/1.0.3/quotes?pairs=EURUSD,EURJPY,GBPUSD,USDCAD,&api_key=KEY')
df = df.pivot_table('price', 'timestamp', 'symbol')
df.to_csv('datapull.csv')
df = pd.read_csv('datapull.csv', index_col='timestamp')
x = range(6)
for n in x:
df2 = pd.read_json('https://forex.1forge.com/1.0.3/quotes?pairs=EURUSD,EURJPY,GBPUSD,USDCAD,&api_key=KEY')
df2 = df2.pivot_table('price', 'timestamp', 'symbol')
df2.to_csv('datapull2.csv')
df2 = pd.read_csv('datapull2.csv', index_col='timestamp')
df3 = df.append(df2)
time.sleep(1)
print(df3)
输入JSON输入数据的示例是:
[{“ symbol”:“ EURUSD”,“ bid”:1.13913,“ ask”:1.13913,“ price”:1.13913,“ timestamp”:1541703878},{“ symbol”:“ EURJPY”,“ bid” :129.75,“询问”:129.753,“价格”:129.7515,“时间戳”:1541703878},{“符号”:“ GBPUSD”,“出价”:1.30907,“询问”:1.30908,“价格”:1.30907,” timestamp“:1541703878},{” symbol“:” USDCAD“,” bid“:1.31059,” ask“:1.31059,” price“:1.31059,” timestamp“:1541703878}]
下面是上面代码的输出。
EURJPY EURUSD GBPUSD USDCAD
timestamp
2018-11-08 00:06:06 129.8615 1.14359 1.31276 1.31167
2018-11-08 00:06:06 129.8610 1.14359 1.31276 1.31167
EURJPY EURUSD GBPUSD USDCAD
timestamp
2018-11-08 00:06:06 129.8615 1.14359 1.31276 1.31167
2018-11-08 00:06:08 129.8605 1.14359 1.31277 1.31163
EURJPY EURUSD GBPUSD USDCAD
timestamp
2018-11-08 00:06:06 129.8615 1.14359 1.31276 1.31167
2018-11-08 00:06:09 129.8660 1.14362 1.31278 1.31156
EURJPY EURUSD GBPUSD USDCAD
timestamp
2018-11-08 00:06:06 129.8615 1.14359 1.31276 1.31167
2018-11-08 00:06:10 129.8725 1.14363 1.31289 1.31155
EURJPY EURUSD GBPUSD USDCAD
timestamp
2018-11-08 00:06:06 129.8615 1.14359 1.31276 1.31167
2018-11-08 00:06:12 129.8750 1.14363 1.31289 1.31155
EURJPY EURUSD GBPUSD USDCAD
timestamp
2018-11-08 00:06:06 129.8615 1.14359 1.31276 1.31167
2018-11-08 00:06:13 129.8735 1.14363 1.31289 1.31155
答案 0 :(得分:1)
您正在以错误的方式累积数据,因为您是在每一回合中将第一行({{$calls->email}}
)附加到刚刚提取的行(df
)。
每轮(df2
)追加的结果在每轮结束时都会被覆盖并丢失。
那呢:
df3
即只是将df = pd.read_json('https://forex.1forge.com/1.0.3/quotes?pairs=EURUSD,EURJPY,GBPUSD,USDCAD,&api_key=KEY')
df = df.pivot_table('price', 'timestamp', 'symbol')
df.to_csv('datapull.csv')
df = pd.read_csv('datapull.csv', index_col='timestamp')
x = range(6)
for n in x:
df2 = pd.read_json('https://forex.1forge.com/1.0.3/quotes?pairs=EURUSD,EURJPY,GBPUSD,USDCAD,&api_key=KEY')
df2 = df2.pivot_table('price', 'timestamp', 'symbol')
df2.to_csv('datapull2.csv')
df2 = pd.read_csv('datapull2.csv', index_col='timestamp')
df = df.append(df2)
time.sleep(1)
print(df)
定义为自身的追加,而刚刚获取的新行(df
)。