Ну, во-первых, вы можете разбить что-то на группы длиной n
, например,
dfgroups = [df[x:x+n] for x in range(0, len(df), n)]
Замените 20000
на n
, и вы получите куски не более 20 000 каждая.Затем вы можете зациклить свой код для каждого элемента в dfgroups
.Также вам нужно, чтобы matches
был его собственным списком, к которому вы можете добавить.И, наконец, для удобства чтения для такой длинной строки вы, вероятно, просто хотите написать mapper
функцию, а не использовать массивную лямбду.
Собрав все это вместе, ваш код можно переписать следующим образом.
df = pd.read_csv('dec 10.csv')
# split df into groups of 20,000
dfgroups = [df[x:x+20000] for x in range(0, len(df), 20000)]
matches = [] # empty list to store matches
for dfgroup in dfgroups:
# a function to replace that long line, more readable
# this function will get redefined every loop, using the new `dfgroup` each iteration
# this is optional, and you can instead keep that long line, replacing `df` with `dfgroup`
def mapper(x):
values = dfgroup['Customer Name'].values
result = difflib.get_close_matches(x, values, n=2, cutoff=0.8))
result = result.apply(pd.Series)
result = result.dropna(axis=0)
return result
match = group['Customer Name'].map(mapper) # passing the function as an argument rather than using a lambda
matches.append(match) # append it to the matches list
Теперь matches
эквивалентно [match1, match2, match3, ...]
и может использоваться как matches[0]
matches[1]
и т. Д.