使用pd.read_csv和转换器的下列格式,我可以从api调用和Ticker符号(这里称为“代码”)中检索数据,但没有去掉前导零。* api键在字符串中更改,将无法工作。
例如:
exchanges = ["SHE"]
for ex in exchanges:
df = pd.read_csv(f'https://eodhistoricaldata.com/api/eod-bulk-last-day/{ex}?
api_token=5f13457294ba20.004391&date={str(today)}',
converters={'Code': lambda x: str(x)}) 结果:
代码exchange_short_name日期..。关闭adjusted_close体积
000001她2021-11-17 . 18.11 18.11 66464038
000002她2021-11-17 . 19.46 19.46 62374971
现在,当切换到pd.read_json方法时,我不能再使用以前使用的转换器函数来转换为string。其结果是,这些相同的代码正在删除前导零。
exchanges = ["SHE"]
for ex in exchanges:
df = pd.read_json(f'https://eodhistoricaldata.com/api/eod-bulk-last-day/{ex}?
api_token=5f1323ba20.00238991&fmt=json&date={str(today)}')结果:
代码exchange_short_name日期..。关闭adjusted_close体积
1她2021-11-17 . 18.11 18.11 66464038
2她2021-11-17 . 19.46 19.46 62374971
下面是通过浏览器在实际api调用中看到的数据。
[{"code":"000001","exchange_short_name":"SHE","date":"2021-11-17","open":18.15,"high":18.3,"low":17.98,"close":18.11,"adjusted_close":18.11,"volume":66464038},{"code":"000002","exchange_short_name":"SHE","date":"2021-11-17","open":19.23,"high":19.53,"low":19.09,"close":19.46,"adjusted_close":19.46,"volume":62374971},如何将该pd.read_json修改为以字符串形式读取至少一列(代码)?
发布于 2021-11-24 00:26:23
与converters类似,在采用json时,使用传递dict的dtype参数来转换某些列中的值。
df = pd.read_json(f'https://eodhistoricaldata.com/api/...', dtype={'code':str})https://stackoverflow.com/questions/70012462
复制相似问题