【问题标题】:Accessing other than first element in list doesn't work访问列表中第一个元素以外的元素不起作用
【发布时间】:2018-04-24 12:43:25
【问题描述】:

在我的 DataFrame 中,我有一个带有 dicts 的列表。当我这样做时

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

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

【问题讨论】:

  • 索引[5]指的是什么?
  • DataFrame 中的第五个元素
  • @MikalojM 你能添加data.stations.head()的输出吗?
  • @Dark 这里是:0 [{'outlets': [{'connector': 7, 'id': 340304, '... 1 [{'network_id': 8, 'outlets ':[{'连接器':6 ... 10 [{'插座':[{'连接器':10,'id':364859,... 100 [{'插座':[{'连接器':10 , 'id': 329118, ... 101 [{'outlets': [{'connector': 14, 'id': 462278, ...名称:站,dtype:对象
  • 我将其粘贴到问题中

标签: python pandas


【解决方案1】:

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

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,因为如果您观察第三行,则列表中只有一个元素。希望它能消除你的疑问。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2023-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-12
  • 1970-01-01
相关资源
最近更新 更多