无法访问列表中的第一个元素以外的其他元素

米科拉伊

在我的DataFrame中,我有字典列表。当我做

data.stations.apply(lambda x: x)[5]

输出为:

[{'id': 245855,
'outlets': [{'connector': 13, 'id': 514162, 'power': 0},
   {'connector': 3, 'id': 514161, 'power': 0},
   {'connector': 7, 'id': 514160, 'power': 0}]},
 {'id': 245856,
  'outlets': [{'connector': 13, 'id': 514165, 'power': 0},
   {'connector': 3, 'id': 514164, 'power': 0},
   {'connector': 7, 'id': 514163, 'power': 0}]},
 {'id': 245857,
  'outlets': [{'connector': 13, 'id': 514168, 'power': 0},
   {'connector': 3, 'id': 514167, 'power': 0},
   {'connector': 7, 'id': 514166, 'power': 0}]}]

因此,它看起来像列表中的3个字典。

当我做

data.stations.apply(lambda x: x[0] )[5]

它做了应该做的事:

{'id': 245855,
 'outlets': [{'connector': 13, 'id': 514162, 'power': 0},
  {'connector': 3, 'id': 514161, 'power': 0},
  {'connector': 7, 'id': 514160, 'power': 0}]}

但是,当我选择第二或第三个元素时,它不起作用:

data.stations.apply(lambda x: x[1])[5]

这给出了一个错误:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-118-1210ba659690> in <module>()
----> 1 data.stations.apply(lambda x: x[1])[5]

~\AppData\Local\Continuum\Anaconda3\envs\geo2\lib\site-packages\pandas\core\series.py in apply(self, func, convert_dtype, args, **kwds)
   2549             else:
   2550                 values = self.asobject
-> 2551                 mapped = lib.map_infer(values, f, convert=convert_dtype)
   2552 
   2553         if len(mapped) and isinstance(mapped[0], Series):

pandas/_libs/src/inference.pyx in pandas._libs.lib.map_infer()

<ipython-input-118-1210ba659690> in <lambda>(x)
----> 1 data.stations.apply(lambda x: x[1])[5]

IndexError: list index out of range

为什么?它应该给我第二个要素。

巴拉斯

原因很简单,每一行中的所有列表条目的长度可能都不相同。让我们考虑一个例子

data = pd.DataFrame({'stations':[[{'1':2,'3':4},{'1':2,'3':4},{'1':2,'3':4}],
                                [{'1':2,'3':4},{'1':2,'3':4}],
                                [{'1':2,'3':4}],
                                 [{'1':2,'3':4},{'1':2,'3':4},{'1':2,'3':4}]]
                    })

                                         stations
0  [{'1': 2, '3': 4}, {'1': 2, '3': 4}, {'1': 2, ...
1               [{'1': 2, '3': 4}, {'1': 2, '3': 4}]
2                                 [{'1': 2, '3': 4}]
3  [{'1': 2, '3': 4}, {'1': 2, '3': 4}, {'1': 2, ...

如果您这样做:

data['stations'].apply(lambda x: x[0])[3]

你会得到 :

{'1': 2, '3': 4}

但是,如果您这样做:

data['stations'].apply(lambda x: x[1])[3]

您将得到,Index Error... list out of bounds因为如果您观察到第三行,则列表中只有一个元素。希望它清除您的疑问。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何移动除数组Javascript中的第一个元素以外的内容?

将数组中的第一个匹配元素存储在其他数组中

如何使用列表切片从列表中获取除第一个元素以外的所有内容

如何从数组中删除除JavaScript中的第一个元素以外的所有元素

在javascript数组中,如何获取除第一个元素以外的最后5个元素?

Python:嵌套列表中的第一个元素

flex-direction:第一个元素以外的column-reverse

Python:如果第一个元素等于其他元素一起的元组

访问列表[F#]列表中的第一个元素

嵌套列表中的第一个元素的总和

Ansible从列表中获取第一个元素

提取列表中列表的第一个元素

如何在Matlab中获取矩阵的除第一个元素以外的所有元素?

如何使第一个元素与第一个元素的右侧对齐,以及其他元素在第一个元素的左侧对齐?

列表中的第一个元素haskell

返回Haskell中列表的第一个元素

如何基于第一个元素以各种方式将列表配对

为除第一个元素以外的所有元素实现代码

需要一个数组,其中新数组的第一个元素=除第一个元素以外的所有元素的和

在列表中删除元组的第一个元素

数组中除第一个和最后一个元素外的其他元素

如果数组中的第一个元素做其他事情

如何将数组的第一个元素与其中的每个其他元素耦合以获得元组列表?

如何使列表的最后一个元素以点结尾,其他元素以逗号结尾?

计算嵌套列表中的第一个元素

列表/嵌套列表中的第一个元素

如何检查数组中的每个元素以查看它是否存在于另一个数组中,并将第一个数组中的元素替换为其他元素?

如何访问嵌套列表的第一个元素?

如何获取列表中字符串的第一个元素以及列表中的其余元素?