猿问

拆分大量数据并为所有数据块循环相同的代码

我的问题有点棘手。我已经将我的庞大数据文件分解成块,并多次对每个块应用模糊模糊的代码。之后,我将结果整理到一个文件中。我想知道是否可以应用某种循环来重用代码而不是为每个变量编写它。下面是示例。


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)

我正在寻找一种优化的方法来编写一次匹配代码,而不是为每个数据块编写它,然后稍后对其进行整理。


慕容708150
浏览 177回答 2
2回答

米琪卡哇伊

您可以遍历数据框列表,以便在每次迭代时您只需引用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)
随时随地看视频慕课网APP

相关分类

Python
我要回答