对熊猫对象使用split索引时出现关键错误

时间:2020-09-21 16:14:14

标签: python-3.x pandas

我有以下数据:

    index   Simulation no#  Line    Load    Wind1   Wind2   Wind3   flag    Sequence
0   12  7.0 Line 16 - 19    0.7 0.0 0.0 0.0 1.0 [('G 05', 'Over-Speed', '1.70')]
1   38  20.0    Line 01 - 02    0.7 0.0 0.0 0.0 1.0 [('G 01', 'Under-Speed', '5.00')]
2   60  31.0    Line 01 - 39    0.7 0.0 0.0 0.0 1.0 [('G 02', 'UV', '2.73'), ('G 01', 'Under-Speed...
3   80  41.0    Line 16 - 19    0.7 0.0 0.0 0.2 1.0 [('G 05', 'Over-Speed', '1.72')]

最后一列是对象类型。我希望提取这些条目中的最后一个字符串,它实际上是一个时间值。因此,例如,在我的第一行中,该条目是: [('G 05','Over-Speed','1.70')],我想提取'1.70'。为了说明我的问题,我做了以下工作:

test = df['Sequence'][0:1]
test

12    [('G 05', 'Over-Speed', '1.70')]
Name: Sequence, dtype: object

通过在“超速”上进行分割,我应该能够在任一侧获取字符串,但是当我执行以下命令时,我会得到随后的错误。

test.str.split('Over-Speed')[1]
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-205-8a3c145d0c99> in <module>
----> 1 test.str.split('Over-Speed')[1]

~/anaconda3/lib/python3.7/site-packages/pandas/core/series.py in __getitem__(self, key)
    869         key = com.apply_if_callable(key, self)
    870         try:
--> 871             result = self.index.get_value(self, key)
    872 
    873             if not is_scalar(result):

~/anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_value(self, series, key)
   4403         k = self._convert_scalar_indexer(k, kind="getitem")
   4404         try:
-> 4405             return self._engine.get_value(s, k, tz=getattr(series.dtype, "tz", None))
   4406         except KeyError as e1:
   4407             if len(self) > 0 and (self.holds_integer() or self.is_boolean()):

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

KeyError: 1

有人可以帮助我了解这里出了什么问题吗?

0 个答案:

没有答案