[英]How can you call a second function while using a lambda function in an .apply() call in Python?
我想知道如何在 Pandas 中將 for 循環轉換為 a.apply() 方法。 我正在嘗試遍歷 dataframe (df1) 的一列,並從第二個 dataframe (df2) 的子集中返回匹配項。 我有一個 function 來進行匹配(匹配),還有一個 function 到 select 來自 df2(過濾器)的右子集。 我想知道是否可以使用 Pandas'.apply() 方法來調用這兩個函數。
我已經想出了如何將其作為一個 for 循環來執行(見下文),似乎我可以通過首先創建一個完整的 function 來通過列表理解來做到這一點(見這里),但我無法通過Pandas.apply() 方法和 lambda 表達式。
## Here is my Filter, which selects titles from df2 for one year
## either side of a target year
def Filter (year):
years = [year-1, year, year+1]
return df2[df2.year.isin(years)].title
# Here is my matching routine, it uses the process method from
# fuzzywuzzy
def Matcher(title, choices):
title_match, percent_match, match3 = process.extractOne(title,
choices, scorer=fuzz.token_sort_ratio)
return title_match
# Here is my solution using a for-loop
for index, row in df1.iterrows():
targets = Filter(row.year)
df1.loc[index,'return_match'] = Matcher(row.title, targets)
# Here's my attempt with a lambda function
df1['new_col'] = df1.title.apply(lambda x: Matcher(x,
Filter(df1.year)))
當我使用 lambda function 時,似乎發生的是過濾器 function 僅在第一次迭代時被調用,因此每次迭代都匹配第一個過濾的標題集方法()。 有沒有辦法解決這個問題?
歡迎來到 SO JP。 我在這一行看到一個問題:
# Here's my attempt with a lambda function
df1['new_col'] = df1.title.apply(lambda x: Matcher(x, Filter(df1.year)))
您在所有 DataFrame 列year
上調用Filter
,而作為您的 for 循環解決方案,您只想在該行的年份調用它。 所以我建議在這樣的行上使用 apply :
df1['new_col'] = df1.apply(lambda row: Matcher(row.title, Filter(row.year)), axis=1)
我希望這有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.