包含Pandas的Python,索引和数据框架

时间:2013-03-14 15:39:03

标签: python pandas

我是Python的新手,对索引和数据框有疑问。我有3个源文件,可以用三个键(district_code,district_name,district_type)唯一标识。我正在验证源文件和数据库中的数据之间的数据。下面的代码只在源文件和数据库之间找到匹配的dist_name和dist_code,然后根据可用的值匹配一个与另一个匹配。我需要在函数中添加另一个条件,添加一个额外的索引/键(zone_type),它将使dist唯一匹配并比较所有三个键。

*编辑** 为了简化这一点,我改变了我的方法中的逻辑。通过连接two_code和district_type这两个键,我有一个唯一的标识符(dist_key)。我更改了下面的函数来反映这个变化但是我收到了一个“keyError:你没有名为dist_key的项目”。这个函数产生错误,因为我相信新的唯一标识符只在这个函数中定义。作为这种语言和脚本的新手,我不确定如何在函数外部调用所需的变量(dist_key)。

if dfyearfound:
    df2['district_name']=df2['district_name'].str.strip()
    df2['district_code']=df2['district_code'].str.strip()
    **df2['dist_key']=df2['dist_key'].str.strip()**  """This line is causing the error"""

def addNamesCodes(testframe,districtnamedata,districtcodedata):
        """ Function that will correct any missing data such as district names or district codes.  Parameter is a pandas dataframe, dictionaries which map the district names and district codes """


        #contain list of correct district codes and district names
        districtnames=[]
        districtkeys=[]
        #Non matches
        fdistrictnames=[]
        fdistrictkeys=[]
        #fill empty values in names and codes

        testframe['district_name']=testframe['district_name'].apply(lambda x: str(x))
        testframe['district_name']=testframe['district_name'].fillna('')
        testframe['district_code']=testframe['district_code'].fillna('')
        testframe['dist_key']=testframe['dist_key'].fillna('')
        testframe['dist_key']=testframe['dist_key']+testframe['district_code']
        #Create two new columns containing the district names and district codes in same format as enrollment and teacher data  
        for i in range(len(testframe.index)):
            #both district code and district name are present
            if districtnamedata.has_key(testframe['dist_key'][testframe.index[i]]) and districtcodedata.has_key(testframe['district_name'][testframe.index[i]]):
                #district code and district name are a match
                if ((districtnamedata[testframe['dist_key'][testframe.index[i]]]==testframe['district_name'][testframe.index[i]]) and (districtcodedata[testframe['district_name'][testframe.index[i]]]==testframe['dist_key'][testframe.index[i]])):
                    districtnames.append(districtnamedata[testframe['dist_key'][testframe.index[i]]])
                    districtkeys.append(districtcodedata[testframe['district_name'][testframe.index[i]]])
                #potential wrong mappings
                else:
                    districtkeys.append(testframe['dist_key'][testframe.index[i]])
                    districtnames.append(districtnamedata[testframe['dist_key'][testframe.index[i]]])
            else:
                #check if district code is present
                if districtnamedata.has_key(testframe['dist_key'][testframe.index[i]]):
                    districtkeys.append(testframe['dist_key'][testframe.index[i]])
                    districtnames.append(districtnamedata[testframe['dist_key'][testframe.index[i]]])
                #check if only district name is present 
                elif districtcodedata.has_key(testframe['district_name'][testframe.index[i]]):
                    districtnames.append(testframe['district_name'][testframe.index[i]])
                    districtkeys.append(districtcodedata[testframe['district_name'][testframe.index[i]]])
                #complete nonmatches
                else:
                    fdistrictnames.append(testframe['district_name'][testframe.index[i]])
                    fdistrictkeys.append(testframe['dist_key'][testframe.index[i]])
        #extend the list by the complete nonmatches
        districtnames.extend(fdistrictnames)
        districtkeys.extend(fdistrictkeys)

        return districtnames,districtkeys

示例来源: 招收---

district_code   district_name   district_type_code  enroll_totals
1                 AITKIN               1                    122
1                 AITKIN               1                    123
1                 SAVAGE               3                    140
1                 SAVAGE               3                    780
15              ST. FRANCIS            1                    782
16              SPRING LAKE            1                    784

金融---

district_code   district_name   district_type_code          budget
1                 AITKIN               1                    122000
1                 AITKIN               1                    120003
1                 SAVAGE               3                    140000
1                 SAVAGE               3                    780000
15              ST. FRANCIS            1                    782000
16              SPRING LAKE            1                    784000

教师---

district_code   district_name   district_type_code         Salary
1                 AITKIN               1                    50000
1                 AITKIN               1                    42000
1                 SAVAGE               3                    89000
1                 SAVAGE               3                    32000
15              ST. FRANCIS            1                    78000
16              SPRING LAKE            1                    58000

0 个答案:

没有答案