我的問題有點棘手。我已經將我的龐大數據文件分解成塊,并多次對每個塊應用模糊模糊的代碼。之后,我將結果整理到一個文件中。我想知道是否可以應用某種循環來重用代碼而不是為每個變量編寫它。下面是示例。df = pd.read_csv('dec 10.csv')df1 = df.iloc[0:20000]df2 = df.iloc[20000:40000]df3 = df.iloc[40000:60000]match1 = df1['Customer Name'].map(lambda x: difflib.get_close_matches(x, df1['Customer Name'].values, n=2, cutoff=0.8)).apply(pd.Series).dropna(axis=0)match2 = df2['Customer Name'].map(lambda x: difflib.get_close_matches(x, df2['Customer Name'].values, n=2, cutoff=0.8)).apply(pd.Series).dropna(axis=0)match3 = df3['Customer Name'].map(lambda x: difflib.get_close_matches(x, df3['Customer Name'].values, n=2, cutoff=0.8)).apply(pd.Series).dropna(axis=0)a = match1.append(match2, ignore_index =True)b = a.append(match3, ignore_index =True)我正在尋找一種優化的方法來編寫一次匹配代碼,而不是為每個數據塊編寫它,然后稍后對其進行整理。
2 回答

米琪卡哇伊
TA貢獻1998條經驗 獲得超6個贊
您可以遍歷數據框列表,以便在每次迭代時您只需引用df并避免重復代碼:
match = pd.Dataframe()
for df in [df1,df2,df3]:
match_ = df['Customer Name'].map(lambda x: difflib
.get_close_matches(x, df['Customer Name'].values, n=2, cutoff=0.8))
.apply(pd.Series).dropna(axis=0)
match = match.append(match_, ignore_index =True)
添加回答
舉報
0/150
提交
取消