У меня есть два фрейма данных:
[in]print(training_df.head(n=10))
[out]
product_id
transaction_id
0000001 [P06, P09]
0000002 [P01, P05, P06, P09]
0000003 [P01, P06]
0000004 [P01, P09]
0000005 [P06, P09]
0000006 [P02, P09]
0000007 [P01, P06, P09, P10]
0000008 [P03, P05]
0000009 [P03, P09]
0000010 [P03, P05, P06, P09]
[in]print(testing_df.head(n=10))
[out]
product_id
transaction_id
001 [P01]
002 [P01, P02]
003 [P01, P02, P09]
004 [P01, P03]
005 [P01, P03, P05]
006 [P01, P03, P07]
007 [P01, P03, P08]
008 [P01, P04]
009 [P01, P04, P05]
010 [P01, P04, P08]
Каждая строка в testing_df является возможной «подстрокой» строки в training_df. Я хотел бы найти все совпадения и вернуть возможные списки training_df для каждого списка в файле testing_df. Было бы полезно, если бы я мог вернуть словарь, где ключи — это transaction_id из testing_df, а значения — все возможные «совпадения» в training_df. (Каждый список в training_df должен быть на одно значение длиннее, чем соответствующий список в test_df).
Я попытался:
# Find the substrings that match
matches = []
for string in training_df:
results = []
for substring in testing_df:
if substring in string:
results.append(substring)
if results:
matches.append(results)
Однако это не работает, оно возвращает только имя столбца «product_id».
Я также пробовал:
# Initialize a list to store the matches between incomplete testing_df and training_df
matches = {}
# Compare the "incomplete" testing lists to the training set
for line in testing_df.product_id:
for line in training_df.product_id:
if line in testing_df.product_id in line in training_df.product_id:
matches[line] = training_df[training_df.product_id.str.contains(line)]
Однако это вызывает ошибку TypeError: unhashable type: 'list'
matches = {} for testing_id in testing_df.product_id: matches[testing_id] = training_df[training_df.product_id.str.contains(testing_id[1:-1])]
, он просто возвращает пустой словарь 02.08.2017['P01','P02','P03']
02.08.2017