【发布时间】:2020-07-06 04:08:14
【问题描述】:
我在将 JSON 格式的数据转换为 Pandas 数据帧时遇到问题。我的 JSON 数据看起来像这样,并存储在变量 active_assets -
Asset({ 'class': 'us_equity',
'easy_to_borrow': True,
'exchange': 'NYSE',
'id': '879ac630-107f-43ce-a01d-1fd9da89453c',
'marginable': True,
'name': 'Zymeworks Inc.',
'shortable': True,
'status': 'active',
'symbol': 'ZYME',
'tradable': True}), Asset({ 'class': 'us_equity',
'easy_to_borrow': False,
'exchange': 'NASDAQ',
'id': 'a838c0cf-0008-432e-8882-feee5a6ef7cd',
'marginable': True,
'name': 'Zynerba Pharmaceuticals, Inc. Common Stock',
'shortable': False,
'status': 'active',
'symbol': 'ZYNE',
'tradable': True}), Asset({ 'class': 'us_equity',
'easy_to_borrow': True,
'exchange': 'NASDAQ',
'id': '52eed246-61b0-4e82-95a9-1d23906b752e',
'marginable': True,
'name': 'Zynex, Inc. Common Stock',
'shortable': True,
'status': 'active',
'symbol': 'ZYXI',
'tradable': True})
active_assets = api.list_assets(status='active',asset_class='us_equity')
df_dict = [{'SYMBL':i['symbol'], 'NAME':i['name'],'Shortable':i['shortable']} for i in active_assets['Asset']]
extracted_df = pd.DataFrame(df_dict)
当我运行此代码时出现以下错误?任何帮助表示赞赏。
File "D:\Trading-Scripts\Scan-alpaca.py", line 32, in <module>
df_dict = [{'SYMBL':i['symbol'], 'NAME':i['name'],'Shortable':i['shortable']} for i in active_assets['Asset']]
TypeError: list indices must be integers or slices, not str
【问题讨论】:
-
显然
i是一个列表。您可能需要检查它的类型。
标签: python json pandas dictionary