Это неэффективно, но я думаю, что подойдет вам.
from fuzzywuzzy import fuzz
import pandas as pd
import numpy as np
# helper function
def remove_element(lst, index):
"Removes an element from a list based on the index"
tmp = lst.copy()
del tmp[index]
return tmp
df = pd.DataFrame({'col1':['A', 'A', 'A', 'A', 'A', 'A', 'A',
'B', 'B', 'B', 'B', 'B', 'B', 'B'],
'col2':['Miami', 'Miami', 'Miami.', 'Barcelona', 'Barc elona',
'Shanghai', 'Shangai', 'Miami', 'Miami', 'Miami.',
'Barcelona', 'Barc elona', 'Shanghai', 'Shangai'],
'col3':[1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0]})
# create a column that indicates the index of the element within the group
df['col2_index'] = 1
df['col2_index'] = df.groupby('col1')['col2_index'].cumsum() - 1
# create a list of the elements within the group
df['col2_list'] = df['col1'].map(df.groupby('col1')['col2'].apply(list))
df['col3_list'] = df['col1'].map(df.groupby('col1')['col3'].apply(list))
# remove the element associated with col2 and col3 respectively
df['col2_list'] = df.apply(lambda x: remove_element(x['col2_list'], x['col2_index']), axis=1)
df['col3_list'] = df.apply(lambda x: remove_element(x['col3_list'], x['col2_index']), axis=1)
# apply the threshold of 80 for the partial_ratio
df['key'] = df.apply(lambda x:
np.array([fuzz.partial_ratio(x['col2'], el) for el in x['col2_list']]) >= 80, axis=1)
# get the average of col3 for those that pass the threshold
df['result'] = df.apply(lambda x: np.mean(np.array(x['col3_list'])[x['key']]), axis=1)
df
col1 col2 col3 col2_index col2_list col3_list key result
0 A Miami 1 0 [Miami, Miami., Barcelona, Barc elona, Shangha... [0, 0, 0, 0, 1, 0] [True, True, False, False, False, False] 0.0
1 A Miami 0 1 [Miami, Miami., Barcelona, Barc elona, Shangha... [1, 0, 0, 0, 1, 0] [True, True, False, False, False, False] 0.5
2 A Miami. 0 2 [Miami, Miami, Barcelona, Barc elona, Shanghai... [1, 0, 0, 0, 1, 0] [True, True, False, False, False, False] 0.5
3 A Barcelona 0 3 [Miami, Miami, Miami., Barc elona, Shanghai, S... [1, 0, 0, 0, 1, 0] [False, False, False, True, False, False] 0.0
4 A Barc elona 0 4 [Miami, Miami, Miami., Barcelona, Shanghai, Sh... [1, 0, 0, 0, 1, 0] [False, False, False, True, False, False] 0.0
5 A Shanghai 1 5 [Miami, Miami, Miami., Barcelona, Barc elona, ... [1, 0, 0, 0, 0, 0] [False, False, False, False, False, True] 0.0
6 A Shangai 0 6 [Miami, Miami, Miami., Barcelona, Barc elona, ... [1, 0, 0, 0, 0, 1] [False, False, False, False, False, True] 1.0
7 B Miami 1 0 [Miami, Miami., Barcelona, Barc elona, Shangha... [1, 1, 0, 0, 1, 0] [True, True, False, False, False, False] 1.0
8 B Miami 1 1 [Miami, Miami., Barcelona, Barc elona, Shangha... [1, 1, 0, 0, 1, 0] [True, True, False, False, False, False] 1.0
9 B Miami. 1 2 [Miami, Miami, Barcelona, Barc elona, Shanghai... [1, 1, 0, 0, 1, 0] [True, True, False, False, False, False] 1.0
10 B Barcelona 0 3 [Miami, Miami, Miami., Barc elona, Shanghai, S... [1, 1, 1, 0, 1, 0] [False, False, False, True, False, False] 0.0
11 B Barc elona 0 4 [Miami, Miami, Miami., Barcelona, Shanghai, Sh... [1, 1, 1, 0, 1, 0] [False, False, False, True, False, False] 0.0
12 B Shanghai 1 5 [Miami, Miami, Miami., Barcelona, Barc elona, ... [1, 1, 1, 0, 0, 0] [False, False, False, False, False, True] 0.0
13 B Shangai 0 6 [Miami, Miami, Miami., Barcelona, Barc elona, ... [1, 1, 1, 0, 0, 1] [False, False, False, False, False, True] 1.0
Для обновленного вопроса просто удалите часть кода, которая уточняет списки
df = pd.DataFrame({'col1':['A', 'A', 'A', 'A', 'A', 'A', 'A',
'B', 'B', 'B', 'B', 'B', 'B', 'B'],
'col2':['Miami', 'Miami', 'Miami.', 'Barcelona', 'Barc elona',
'Shanghai', 'Shangai', 'Miami', 'Miami', 'Miami.',
'Barcelona', 'Barc elona', 'Shanghai', 'Shangai'],
'col3':[1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0]})
df['col2_list'] = df['col1'].map(df.groupby('col1')['col2'].apply(list))
df['col3_list'] = df['col1'].map(df.groupby('col1')['col3'].apply(list))
df['key'] = df.apply(lambda x:
np.array([fuzz.partial_ratio(x['col2'], el) for el in x['col2_list']]) >= 80, axis=1)
df['result'] = df.apply(lambda x: np.mean(np.array(x['col3_list'])[x['key']]), axis=1)
df
col1 col2 col3 col2_list col3_list key result
0 A Miami 1 [Miami, Miami, Miami., Barcelona, Barc elona, ... [1, 0, 0, 0, 0, 1, 0] [True, True, True, False, False, False, False] 0.333333
1 A Miami 0 [Miami, Miami, Miami., Barcelona, Barc elona, ... [1, 0, 0, 0, 0, 1, 0] [True, True, True, False, False, False, False] 0.333333
2 A Miami. 0 [Miami, Miami, Miami., Barcelona, Barc elona, ... [1, 0, 0, 0, 0, 1, 0] [True, True, True, False, False, False, False] 0.333333
3 A Barcelona 0 [Miami, Miami, Miami., Barcelona, Barc elona, ... [1, 0, 0, 0, 0, 1, 0] [False, False, False, True, True, False, False] 0.000000
4 A Barc elona 0 [Miami, Miami, Miami., Barcelona, Barc elona, ... [1, 0, 0, 0, 0, 1, 0] [False, False, False, True, True, False, False] 0.000000
5 A Shanghai 1 [Miami, Miami, Miami., Barcelona, Barc elona, ... [1, 0, 0, 0, 0, 1, 0] [False, False, False, False, False, True, True] 0.500000
6 A Shangai 0 [Miami, Miami, Miami., Barcelona, Barc elona, ... [1, 0, 0, 0, 0, 1, 0] [False, False, False, False, False, True, True] 0.500000
7 B Miami 1 [Miami, Miami, Miami., Barcelona, Barc elona, ... [1, 1, 1, 0, 0, 1, 0] [True, True, True, False, False, False, False] 1.000000
8 B Miami 1 [Miami, Miami, Miami., Barcelona, Barc elona, ... [1, 1, 1, 0, 0, 1, 0] [True, True, True, False, False, False, False] 1.000000
9 B Miami. 1 [Miami, Miami, Miami., Barcelona, Barc elona, ... [1, 1, 1, 0, 0, 1, 0] [True, True, True, False, False, False, False] 1.000000
10 B Barcelona 0 [Miami, Miami, Miami., Barcelona, Barc elona, ... [1, 1, 1, 0, 0, 1, 0] [False, False, False, True, True, False, False] 0.000000
11 B Barc elona 0 [Miami, Miami, Miami., Barcelona, Barc elona, ... [1, 1, 1, 0, 0, 1, 0] [False, False, False, True, True, False, False] 0.000000
12 B Shanghai 1 [Miami, Miami, Miami., Barcelona, Barc elona, ... [1, 1, 1, 0, 0, 1, 0] [False, False, False, False, False, True, True] 0.500000
13 B Shangai 0 [Miami, Miami, Miami., Barcelona, Barc elona, ... [1, 1, 1, 0, 0, 1, 0] [False, False, False, False, False, True, True] 0.500000