我是 Python 新手,对索引和数据框有疑问。我有 3 个源文件,可以用三个键(区代码、区名称、区类型)唯一标识。我正在验证源文件和数据库中的数据之间的数据。下面的代码仅在源文件和数据库之间找到匹配的 dist_name 和 dist_code,然后根据可用的值将一个与另一个匹配。我需要向函数添加另一个条件,添加一个额外的索引/键(district_type),这将使 dist 唯一匹配并比较所有三个键。
* 编辑** 为了简化这一点,我改变了我的方法中的逻辑。通过连接两个键 District_code 和 District_type 我有一个唯一标识符(dist_key)。我更改了下面的函数以反映此更改,但我收到了"keyError: u'no item named 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