简体   繁体   English

如何从Python字典列表中提取数据

[英]How to pull data from Python List of dictionaries

I am looking to pull the "high" from all 3 lists below. 我希望从下面所有3个列表中拉出“最高”。 I am unsure how to proceed. 我不确定如何进行。

[  
   {  
      'timestamp':'2019-04-09T23:41:00.000Z',
      'symbol':'XBTUSD',
      'open':5189.5,
      'high':5190,
      'low':5189,
      'close':5190,
      'trades':14,
      'volume':1246,
      'vwap':5189.4136,
      'lastSize':480,
      'turnover':24010476,
      'homeNotional':0.24010476,
      'foreignNotional':1246
   },
   {  
      'timestamp':'2019-04-09T23:40:00.000Z',
      'symbol':'XBTUSD',
      'open':5190,
      'high':5190,
      'low':5189.5,
      'close':5189.5,
      'trades':4,
      'volume':540,
      'vwap':5189.9523,
      'lastSize':20,
      'turnover':10404800,
      'homeNotional':0.104048,
      'foreignNotional':540
   },
   {  
      'timestamp':'2019-04-09T23:39:00.000Z',
      'symbol':'XBTUSD',
      'open':5197.5,
      'high':5195.5,
      'low':5187,
      'close':5190,
      'trades':56,
      'volume':24286,
      'vwap':5189.6829,
      'lastSize':1058,
      'turnover':467970327,
      'homeNotional':4.67970327,
      'foreignNotional':24286
   }
]

I have tried to simply use high = [0:]["high"] - but that returns 我试图简单地使用high = [0:]["high"] -但这会返回

 TypeError: list indices must be integers or slices, not str

What would be the appropriate command to have all 3 "high" from each list? 将每个列表中的所有3个“高”都包含在其中的适当命令是什么? For this instance, it should return 5190, 5190, 5195.5 . 对于此实例,它应返回5190, 5190, 5195.5

使用列表理解: [ x['high'] for x in data ]如果data是您在原始帖子中发布的数组。

I think below answer will help 我认为下面的答案会有所帮助

I have assigned your list to listofDict and I am using the for loop to iterate through all the list elements . 我已经将您的列表分配给listofDict并且正在使用for循环遍历所有list元素。

As listofDict element are dict type, I am using the key to extract the value . 由于listofDict元素是dict类型,因此我正在使用提取

Here key is High 这里

listofDict=[  
   {  
      'timestamp':'2019-04-09T23:41:00.000Z',
      'symbol':'XBTUSD',
      'open':5189.5,
      'high':5190,
      'low':5189,
      'close':5190,
      'trades':14,
      'volume':1246,
      'vwap':5189.4136,
      'lastSize':480,
      'turnover':24010476,
      'homeNotional':0.24010476,
      'foreignNotional':1246
   },
   {  
      'timestamp':'2019-04-09T23:40:00.000Z',
      'symbol':'XBTUSD',
      'open':5190,
      'high':5190,
      'low':5189.5,
      'close':5189.5,
      'trades':4,
      'volume':540,
      'vwap':5189.9523,
      'lastSize':20,
      'turnover':10404800,
      'homeNotional':0.104048,
      'foreignNotional':540
   },
   {  
      'timestamp':'2019-04-09T23:39:00.000Z',
      'symbol':'XBTUSD',
      'open':5197.5,
      'high':5195.5,
      'low':5187,
      'close':5190,
      'trades':56,
      'volume':24286,
      'vwap':5189.6829,
      'lastSize':1058,
      'turnover':467970327,
      'homeNotional':4.67970327,
      'foreignNotional':24286
   }
]


for l in listofDict:
   print (l['high'])

You can use pandas and convert to dataframe /table and then you can get df["high"] . 您可以使用pandas并将其转换为dataframe / table,然后可以获取df["high"]

If you will need only two values then you can do df['high'][:2] . 如果只需要两个值,则可以执行df['high'][:2] It is similar to what you tried. 这与您尝试过的类似。

import pandas as pd

data = [  
   {  
      'timestamp':'2019-04-09T23:41:00.000Z',
      'symbol':'XBTUSD',
      'open':5189.5,
      'high':5190,
      'low':5189,
      'close':5190,
      'trades':14,
      'volume':1246,
      'vwap':5189.4136,
      'lastSize':480,
      'turnover':24010476,
      'homeNotional':0.24010476,
      'foreignNotional':1246
   },
   {  
      'timestamp':'2019-04-09T23:40:00.000Z',
      'symbol':'XBTUSD',
      'open':5190,
      'high':5190,
      'low':5189.5,
      'close':5189.5,
      'trades':4,
      'volume':540,
      'vwap':5189.9523,
      'lastSize':20,
      'turnover':10404800,
      'homeNotional':0.104048,
      'foreignNotional':540
   },
   {  
      'timestamp':'2019-04-09T23:39:00.000Z',
      'symbol':'XBTUSD',
      'open':5197.5,
      'high':5195.5,
      'low':5187,
      'close':5190,
      'trades':56,
      'volume':24286,
      'vwap':5189.6829,
      'lastSize':1058,
      'turnover':467970327,
      'homeNotional':4.67970327,
      'foreignNotional':24286
   }
]

df = pd.DataFrame(data)

print(df['high'].to_list())

[5190.0, 5190.0, 5195.5]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM