如果我格式错误或以其他方式破坏了我的问题,请提前道歉......
我是 pandas(和 python)的新手,并且一直在慢慢地尝试将学到的东西应用到我自己的数据集上。
我有一个稀疏的授权数据数据框,其中许多单元格为空(或等效),其中许多应该能够通过规范化来消除。此外,每个项目编号都包含可用于验证其他单独提供的字段(年份、授予人、研究活动等)的信息。
项目编号如下所示:
0 None,
1 None,
2 3C06RR018774,
3 3C06RR018774,
4 1C06RR018796,
5 1C06RR018796,
6 3C06RR018863,
7 3C06RR018863,
8 1C06RR020099,
9 1C06RR020099,
10 1C06RR020117,
我编写的正则表达式看起来像:
reobj = re.compile(r"""
(?P<type>[0-9]+)
(?P<act>[A-Z]+[0-9]+)
(?P<ic>[A-Z]{2})
(?P<serial>[0-9]{1,6})""",
flags=re.X)
我很确定这是可行的,因为:
m = core.str.match(reobj)
给出:
0 None
1 None
2 (3, C06, RR, 018774)
3 (3, C06, RR, 018774)
4 (1, C06, RR, 018796)
5 (1, C06, RR, 018796)
6 (3, C06, RR, 018863)
7 (3, C06, RR, 018863)
8 (1, C06, RR, 020099)
9 (1, C06, RR, 020099)
10 (1, C06, RR, 020117)
但是我无法取出组 m.groups():
AttributeError: 'Series' object has no attribute 'groups'
我似乎也无法挖掘出解析的位,m.str.get(1):我也得到与 m.str[0] 相同的错误
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-44-2a7675586aff> in <module>()
----> 1 m.str.get(1)
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/strings.pyc in get(self, i)
668 @copy(str_get)
669 def get(self, i):
--> 670 result = str_get(self.series, i)
671 return self._wrap_result(result)
672
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/strings.pyc in str_get(arr, i)
551 """
552 f = lambda x: x[i]
--> 553 return _na_map(f, arr)
554
555
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/strings.pyc in _na_map(f, arr, na_result)
87 def _na_map(f, arr, na_result=np.nan):
88 # should really _check_ for NA
---> 89 return _map(f, arr, na_mask=True, na_value=na_result)
90
91
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/strings.pyc in _map(f, arr, na_mask, na_value)
96 mask = isnull(arr)
97 try:
---> 98 result = lib.map_infer_mask(arr, f, mask.view(np.uint8))
99 except (TypeError, AttributeError):
100 def g(x):
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/lib.so in pandas.lib.map_infer_mask (pandas/lib.c:39584)()
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/strings.pyc in <lambda>(x)
550 items : array
551 """
--> 552 f = lambda x: x[i]
553 return _na_map(f, arr)
554
IndexError: list index out of range
但是, m.str[:1] 确实挖掘出来:
0 None
1 None
2 (3)
3 (3)
4 (1)
5 (1)
6 (3)
7 (3)
8 (1)
9 (1)
10 (1)
我认为我的主要误解是编译匹配对象在系列中的迭代,但归根结底,我希望能够将每个组与同一行中的另一个字段进行比较,以在内部验证数据集:
m.group('ic') == data['IC']
虽然理想情况下能够跨行以类似数组的方式一次比较所有捕获的组......
任何指导,甚至只是朝着正确方向健康的推动,将不胜感激......