我认为这对有熊猫经验的人来说非常容易。 我只想从OTHERNAMES列中找到字符串是子字符串的行。 我更喜欢使用lambda函数的解决方案。
数据框是这样的:
ID NAME OTHERNAMES COUNTRY GPS_LEN GPS_LAT SPORT 1 Manchester City manchestercity|manchestercityfc|mancity England 53.483056 -2.200278 football 2 Leicester leicester|leicestercityfc England 52.620278 -1.142222 football
我希望这样的事情应该起作用:
teams_dict = pd.read_csv(file_path.csv)
teams_dict.OTHERNAMES[lambda s: s.str.split('|').isin(['mancity'])]
我不想使用s.str.contains('mancity'),因为可能会有另一个名称为mancityb的团队,不应返回。分隔符'|'是重要的一部分。
但是我得到这个错误:
TypeError Traceback (most recent call last)
TypeError: unhashable type: 'list'
The above exception was the direct cause of the following exception:
SystemError Traceback (most recent call last)
<ipython-input-149-b21630ad3067> in <module>
----> 1 teams_dict.OTHERNAMES[lambda s: s.str.split('|').isin(['mancity'])]
c:\program files\python36\lib\site-packages\pandas\core\series.py in __getitem__(self, key)
763
764 def __getitem__(self, key):
--> 765 key = com._apply_if_callable(key, self)
766 try:
767 result = self.index.get_value(self, key)
c:\program files\python36\lib\site-packages\pandas\core\common.py in _apply_if_callable(maybe_callable, obj, **kwargs)
406
407 if callable(maybe_callable):
--> 408 return maybe_callable(obj, **kwargs)
409
410 return maybe_callable
<ipython-input-149-b21630ad3067> in <lambda>(s)
----> 1 teams_dict.OTHERNAMES[lambda s: s.str.split('|').isin(['mancity'])]
c:\program files\python36\lib\site-packages\pandas\core\series.py in isin(self, values)
3588 Name: animal, dtype: bool
3589 """
-> 3590 result = algorithms.isin(self, values)
3591 return self._constructor(result, index=self.index).__finalize__(self)
3592
c:\program files\python36\lib\site-packages\pandas\core\algorithms.py in isin(comps, values)
444 comps = comps.astype(object)
445
--> 446 return f(comps, values)
447
448
c:\program files\python36\lib\site-packages\pandas\core\algorithms.py in <lambda>(x, y)
419
420 # faster for larger cases to use np.in1d
--> 421 f = lambda x, y: htable.ismember_object(x, values)
422
423 # GH16012
pandas\_libs\hashtable_func_helper.pxi in pandas._libs.hashtable.ismember_object()
SystemError: <built-in method view of numpy.ndarray object at 0x000002A9BA12CDA0> returned a result with an error set