应用唯一后如何获取原始数据帧中的行索引

时间:2018-09-03 12:05:19

标签: python pandas dataframe

我有一个数据框,我正在对其列中的一个应用唯一方法,但是

for i in range(len(np.unique(first_group.Note_Detail))):
     s=np.unique(first_group.Note_Detail)[i]
     first_group.Insurance_Id[i]     
         t={}
for m in re.finditer(r'^(.+)(?:\r?\n\s*\[(.+)])?\r?$', s, re.M):


    if m.group(2):
        t1={}
        for sub_item in [x.strip() for x in m.group(2).strip().split('], [') if ':' in x]:
            itm=sub_item.split(':')
            itm1=itm[1].strip().split('to')
            t2={}
            if len(itm1)>1:
                t2['old']=itm1[0].strip()
                t2['new']=itm1[1].strip()
                t2['action']='Changed'
            elif len(itm1)==1 and itm[1].strip()=='Removed':
                t2['old']='Null'
                t2['new']='Null'
                t2['action']='Removed'    
            else:
                t2['old']='Null'
                t2['new']=itm1[0].strip()
                t2['action']='added'
            t1[itm[0].strip()]=t2   
#         t.append(t1)
        t[m.group(1).strip().split(':')[0]]=t1
    else:
        t[m.group(1).strip().split(':')[0]]='None'
#     print t    
   res[i]=[t,first_group.Insurance_Id[i],first_group.Patient_Id,first_group.Patient_Id,first_group.Created_By]

打印资源

  
KeyError                                  Traceback (most recent call last)
<ipython-input-314-971c32825ccc> in <module>()
     31             t[m.group(1).strip().split(':')[0]]='None'
     32     #     print t
---> 33     res[i]=[t,first_group.Insurance_Id[i],first_group.Patient_Id,first_group.Patient_Id,first_group.Created_By]
     34 print res

/usr/local/lib/python2.7/dist-packages/pandas/core/series.pyc in __getitem__(self, key)
    765         key = com._apply_if_callable(key, self)
    766         try:
--> 767             result = self.index.get_value(self, key)
    768 
    769             if not is_scalar(result):

/usr/local/lib/python2.7/dist-packages/pandas/core/indexes/base.pyc in
     

get_value(自身,系列,键)          3116试试:          3117返回self._engine.get_value(s,k,       -> 3118 tz = getattr(series.dtype,'tz',无)          3119,除了KeyError作为e1:          3120如果len(self)> 0并且self.inferred_type为['integer','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: 0

如何获取first_group数据帧的索引以在循环内访问first_group.Insurance_Id[i]

0 个答案:

没有答案