Pandas compare each row with all rows in data frame and save results in list for each row

梦想与她 提交于 2019-12-04 13:35:01

问题


I try compare each row with all rows in pandas DF through fuzzywuzzy.fuzzy.partial_ratio() >= 85 and write results in list for each row.

in: df = pd.DataFrame( {'id':[1, 2, 3, 4, 5, 6], 'name':['dog', 'cat', 'mad cat', 'good dog', 'bad dog', 'chicken']})

use pandas function with fuzzywuzzy library get result:

out: 
    id  name     match_id_list
    1   dog      [4, 5]
    2   cat      [3, ]
    3   mad cat  [2, ]
    4   good dog [1, 5]
    5   bad dog  [1, 4]
    6   chicken  []

But I don't understand how get this.


回答1:


The first step would be to find the indices that match the condition for a given name. Since partial_ratio only takes strings, we apply it to the dataframe:

name = 'dog'
df.apply(lambda row: (partial_ratio(row['name'], name) >= 85), axis=1)

We can then use enumerate and list comprehension to generate the list of true indices in the boolean array:

matches = df.apply(lambda row: (partial_ratio(row['name'], name) >= 85), axis=1)
[i for i, x in enumerate(matches) if x]

Let's put all this inside a function:

def func(name):
    matches = df.apply(lambda row: (partial_ratio(row['name'], name) >= 85), axis=1)
    return [i for i, x in enumerate(matches) if x]

We can now apply the function to the entire dataframe:

df.apply(lambda row: func(row['name']), axis=1)


来源:https://stackoverflow.com/questions/35459316/pandas-compare-each-row-with-all-rows-in-data-frame-and-save-results-in-list-for

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!