【发布时间】:2018-12-10 10:11:20
【问题描述】:
我的问题有点棘手。我已经将我的巨大数据文件分成几块,并多次对每个块应用模糊的代码。之后,我将结果整理到一个文件中。我想知道是否可以应用某种循环来重用代码,而不是为每个变量编写代码。下面是例子。
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)
我正在寻找一种优化的方式来编写一次匹配代码,而不是为每个数据块编写它,然后再进行整理。
【问题讨论】:
-
这正是函数的用途。你熟悉函数的工作原理吗?在您的示例中,即使是一个简单的 for 循环也会有所帮助。
-
是的,我可以编写直接代码,但不能编写函数。现在正在检查,并会试一试......