简体   繁体   中英

How to pull data from Python List of dictionaries

I am looking to pull the "high" from all 3 lists below. 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

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

What would be the appropriate command to have all 3 "high" from each list? For this instance, it should return 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 .

As listofDict element are dict type, I am using the key to extract the value .

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"] .

If you will need only two values then you can do 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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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