如何从存在给定子字符串的pandas系列字符串中选择此类实例?

时间:2019-07-04 16:27:43

标签: python pandas dataframe

我试图从pandas series的具有特定子字符串的字符串中找出此类实例。例如:在下面的dataframe中,

Dataframe

我想知道car_data中的哪些实例具有Cr作为car_data['New_Price']中存在的子字符串。 (某些值例如18.65 LakhNaN1.28 Cr)。(我想进一步了解将1.28 Cr作为car_data['New_Price']的实例)

我尝试了以下代码:

instances=car_data['New_Price']
for instance in instances:
    if('Cr' in instance):
        car_data.loc[instance]

但这给了我错误:

KeyError                                  Traceback (most recent call last)
F:\Users\lenovo\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2656             try:
-> 2657                 return self._engine.get_loc(key)
   2658             except KeyError:

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

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

pandas/_libs/index_class_helper.pxi in pandas._libs.index.Int64Engine._check_type()

KeyError: '1.28 Cr'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-265-ec5cde3ce12d> in <module>
      2 for instance in instances:
      3     if('Cr' in instance):
----> 4         car_data.loc[instance]

F:\Users\lenovo\Anaconda3\lib\site-packages\pandas\core\indexing.py in __getitem__(self, key)
   1498 
   1499             maybe_callable = com.apply_if_callable(key, self.obj)
-> 1500             return self._getitem_axis(maybe_callable, axis=axis)
   1501 
   1502     def _is_scalar_access(self, key):

F:\Users\lenovo\Anaconda3\lib\site-packages\pandas\core\indexing.py in _getitem_axis(self, key, axis)
   1911         # fall thru to straight lookup
   1912         self._validate_key(key, axis)
-> 1913         return self._get_label(key, axis=axis)
   1914 
   1915 

F:\Users\lenovo\Anaconda3\lib\site-packages\pandas\core\indexing.py in _get_label(self, label, axis)
    139             raise IndexingError('no slices here, handle elsewhere')
    140 
--> 141         return self.obj._xs(label, axis=axis)
    142 
    143     def _get_loc(self, key, axis=None):

F:\Users\lenovo\Anaconda3\lib\site-packages\pandas\core\generic.py in xs(self, key, axis, level, drop_level)
   3583                                                       drop_level=drop_level)
   3584         else:
-> 3585             loc = self.index.get_loc(key)
   3586 
   3587             if isinstance(loc, np.ndarray):

F:\Users\lenovo\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2657                 return self._engine.get_loc(key)
   2658             except KeyError:
-> 2659                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2660         indexer = self.get_indexer([key], method=method, tolerance=tolerance)
   2661         if indexer.ndim > 1 or indexer.size > 1:

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

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

pandas/_libs/index_class_helper.pxi in pandas._libs.index.Int64Engine._check_type()

KeyError: '1.28 Cr'

能帮我一下吗?我非常需要。

0 个答案:

没有答案