私は自分のスクリプトをデバッグしており、問題を引き起こしていると思われる数行のコードに問題を絞り込みました。3 つの csv ファイルからデータを読み込み、SQL Server の sproc からデータを抽出し、両方のデータを Excel ファイルにエクスポートして cmparison を描画しています。私が得ている問題は、ソース ファイルが重複 (各ソース ファイルから 1 行) を生成していることです。次のデータに印刷ステートメントを入れて、何が起こっているかを確認しました。
#convert district codes to strings
if dfyearfound:
df2['district_code']=df2['district_code'].apply(lambda x: str(x))
print df2['district_code'][df2.index[0]]
df2['district_type_code']=df2['district_type_code'].apply(lambda x: str(x))
print df2['district_type_code'][df2.index[0]]
if teacheryearfound:
teacherframe['district_code']=teacherframe['district_code'].apply(lambda x: str(x))
print teacherframe['district_code'][teacherframe.index[0]]
teacherframe['district_type_code']=teacherframe['district_type_code'].apply(lambda x: str(x))
print teacherframe['district_type_code'][teacherframe.index[0]]
if financialyearfound:
financialframe['district_code']=financialframe['district_code'].apply(lambda x: str(x))
print financialframe['district_code'][financialframe.index[0]]
financialframe['district_type_code']=financialframe['district_type_code'].apply(lambda x: str(x))
print financialframe['district_type_code'][financialframe.index[0]]
print ステートメントにより、次の出力が得られます: 1, 1, 1, 3.0, 0012, 1
すべての dist_codes の長さは 4 である必要があり、ソース ファイル内で 1 桁から 4 桁まで変化します。データベースでは、それらはすべて 4 桁です (例: 0001、0012)。地区タイプは 1 桁または 2 桁で、データベースではすべて 2 です (例: 01、03)。上記の文字列変換が機能しない理由がわかりません。地区コードと地区タイプコードをフォーマットする関数を書くつもりでしたが、長さと関数をハードコードしたくありませんでした。
#function for formating district codes
def formatDistrictCodes(code):
dist=code
dist.zfill(4)
return dist
formatDistrictCodes(districtformat)