简体   繁体   English

Python Pandas从列表中的dict提取值

[英]Python pandas extract values from dict within a list

Basically I would like to extract price value from this column, but the problem that if I use index [0] it would bring index[0] error in line 11 and price key also does not exit in this line. 基本上,我想从此列中提取价格值,但是如果我使用索引[0],它将在第11行中带来index [0]错误,并且价格键也不会在此行中退出的问题。

ex
0    {u'availableToBack': [{u 'price' : 1.02, u 'size' : 2.15}], u 'availableToLay' : [], u 'tradedVolume' : []}
1    {u'availableToBack': [{u'price': 6.8, u'size':...
2    {u'availableToBack': [{u'price': 8.2, u'size':...
3    {u'availableToBack': [{u'price': 9.6, u'size':...
10   {u'availableToBack': [{u'price': 70.0, u'size'...
11   {u'availableToBack': [], u'availableToLay': []...
12   {u'availableToBack': [], u'availableToLay': []...

I use something like this 我用这样的东西

table['price'] = table['ex']['availableToBack'][0].apply(lambda x: x.get('price'))

Can you help me how to go around it, please? 您能帮我解决这个问题吗?

JSON looks like this: JSON看起来像这样:

    u 'runners' : [{
            u 'status' : u 'ACTIVE',
            u 'handicap' : 0.0,
            u 'selectionId' : 10861647,
            u 'totalMatched' : 0.0,
            u 'adjustmentFactor' : 16.631,
            u 'ex' : {
                u 'availableToBack' : [{
                        u 'price' : 1.02,
                        u 'size' : 2.15
                    }
                ],
                u 'availableToLay' : [],
                u 'tradedVolume' : []
            }
        }, {
            u 'status' : u 'ACTIVE',
            u 'handicap' : 0.0,
            u 'selectionId' : 10861648,
            u 'totalMatched' : 0.0,
            u 'adjustmentFactor' : 13.237,
            u 'ex' : {
                u 'availableToBack' : [{
                        u 'price' : 1.01,
                        u 'size' : 7.11
                    }
                ],
                u 'availableToLay' : [],
                u 'tradedVolume' : []
            }
        },
10   {u'availableToBack': [{u'price': 70.0, u'size'...
11   {u'availableToBack': [], u'availableToLay': []...

I just saw that your entry for availableToBack contains an empty list. 我刚刚看到您的availableToBack条目包含一个空列表。 Put in a test which will set it to [dict()] if the list value is empty. 进行测试,如果列表值为空,则将其设置为[dict()]。 This will allow the x.get('price', 0) to work correctly. 这将使x.get('price', 0)正常工作。 Then you will be able to process the value of 'price' as 0 in the rest of your code. 然后,您将可以在其余代码中将“ price”的值处理为0。

You will then not have to test for 'None'. 然后,您将不必测试“无”。

x.get('price') has no "default" value specified so it returns "None". x.get('price')没有指定“默认”值,因此它返回“无”。 Set up a default value (such as x.get('price', 0) ) That way you should be able to perform the operations with a 0 price. 设置默认值(例如x.get('price', 0) ),这样您应该能够以0价格执行操作。

Python dictionary get() Method Python字典get()方法

Description 描述

The method get() returns a value for the given key. 方法get()返回给定键的值。 If key is not available then returns default value None. 如果key不可用,则返回默认值None。 Syntax 句法

Following is the syntax for get() method − 以下是get()方法的语法-

dict.get(key, default=None) dict.get(key,default = None)

Parameters 参量

 key -- This is the Key to be searched in the dictionary. default -- This is the Value to be returned in case key does not exist. 

Return Value 返回值

This method returns a value for the given key. 此方法返回给定键的值。 If key is not available, then it returns the default value None. 如果key不可用,则它将返回默认值None。

I don't think Python has a safe, built-in method for getting a value from a list without triggering an IndexError , but it is easy enough to write one. 我认为Python没有安全,内置的方法可以从列表中获取值而不触发IndexError ,但是编写一个方法很容易。 ( This answer has more detail on that.) 此答案对此有更详细的说明。)

If I understand your intention correctly, this may help you. 如果我正确理解您的意图,这可能会对您有所帮助。 (Note that this only addresses the issue of getting the list item safely. You may want to use .get for each dictionary value you are retrieving for more safety.) (请注意,这仅解决了安全获取列表项的问题。您可能希望对要检索的每个字典值使用.get ,以提高安全性。)

def safeget(seq, index, default=None):
    try:
        return seq[index]
    except IndexError:
        return default

for table in runners:
    price = safeget(table['ex']['availableToBack'], 0, default={}).get('price')
    print price

I tested against this structure, which has a an empty list for u'availableToBack in the second object. 我针对此结构进行了测试,该结构在第二个对象中的u'availableToBack有一个空列表。

runners = [
    {
        u'status' : u'ACTIVE',
        u'handicap' : 0.0,
        u'selectionId' : 10861647,
        u'totalMatched' : 0.0,
        u'adjustmentFactor' : 16.631,
        u'ex' : {
            u'availableToBack' : [
                    {u'price' : 1.02, u'size' : 2.15},
                ],
            u'availableToLay' : [],
            u'tradedVolume' : []
            }
    }, {
        u'status' : u'ACTIVE',
        u'handicap' : 0.0,
        u'selectionId' : 10861648,
        u'totalMatched' : 0.0,
        u'adjustmentFactor' : 13.237,
        u'ex' : {
            u'availableToBack' : [
                ## this one is MISSING a price
            ],
            u'availableToLay' : [],
            u'tradedVolume' : []
        }
    }
]

You can use a ternary operator. 您可以使用三元运算符。 An empty list evaluates to false. 空列表的结果为false。

table['price'] = (table['ex']['availableToBack'][0].get('price') 
                  if table['ex']['availableToBack'] 
                  else None)

I found a solution and decided to post it. 我找到了解决方案,并决定将其发布。 Basically maybe I did not explain well, but the problem is inconsistencies of having anything within the list. 基本上,也许我没有很好地解释,但是问题是列表中没有包含任何内容。

table['availableToBack.price'] = table['ex'].apply(lambda x: x['availableToBack'][0].get('price') if (len(x['availableToBack']) != 0) else None) 

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

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