簡體   English   中英

將用戶定義的 function 應用於 pandas 中的 groupby

[英]apply a user defined function to a groupby in pandas

我的 dataframe df看起來像這樣

review_id   user_id  prod_id    review
0               10      5       this restaurant is the best.
1               30      10      Worst food.
2               10      15      Best place!
3               30      5       the food is too expensive.
4               30      10      Yummy! I love it.

我現在定義了一個 function ACS ,我想用它來計算每個用戶的平均內容相似度。 我寫的function如下:

def ACS(rvw1,rvw2):
    rvw1=rvw1.replace(",", "").replace(".", "").replace("?","").replace("!","").lower()
    rvw2=rvw2.replace(",", "").replace(".", "").replace("?","").replace("!","").lower()
    rvw1words = rvw1.split()
    rvw2words = rvw2.split()
    allwords = list(set(rvw1words) | set(rvw2words))
    rvw1freq=[]
    rvw2freq=[]
    for word in allwords:
        rvw1freq.append(rvw1words.count(word))
        rvw2freq.append(rvw2words.count(word))
    return np.dot(rvw1freq,rvw2freq)/(np.linalg.norm(rvw1freq)*np.linalg.norm(rvw2freq))   

這個 function 將兩個字符串作為輸入,並以 0 到 1 的比例返回它們之間的相似度。我的目的是計算每個用戶的內容相似度,所以我形成了一個 groupby,如下所示:

grouped = df.groupby('user_id')['review']

現在我想在每個組上應用我的ACS function(類似於grouped.ACS() )。 但問題是 ACS 將兩個字符串作為輸入並計算它們的相似度。 但是 groupby 中的每個組可能有超過 2 個評論字符串。 我應該怎么做才能將此 function 應用於每個組,以便它從一個組中獲取所有評論並計算它們的內容相似度。 非常感謝。

您可以使用pd.merge獲取行的笛卡爾積,然后使用pd.DataFrame.apply應用您的 function:

import pandas as pd

# Helper function to get combinations of a dataframe and their cosine similarity
def groupSimilarity(df):
    combinations = (df.assign(dummy=1)
                     .merge(df.assign(dummy=1), on="dummy")
                     .drop("dummy", axis=1))
    similarity = combinations.apply(lambda x: ACS(x["review_x"], x["review_y"]), axis=1)
    combinations.loc[:, "similarity"] = similarity
    return combinations

# apply function to each group
grouped = (df.groupby("user_id")
            .apply(combinations)
            .reset_index())

# >>> grouped[["review_id_x", "review_id_y", "user_id_x", "user_id_y", "distance"]]
#     review_id_x  review_id_y  user_id_x  user_id_y  distance
# 0             0            0         10         10  1.000000
# 1             0            2         10         10  0.316228
# 2             2            0         10         10  0.316228
# 3             2            2         10         10  1.000000
# 4             1            1         30         30  1.000000
# 5             1            3         30         30  0.316228
# 6             1            4         30         30  0.000000
# 7             3            1         30         30  0.316228
# 8             3            3         30         30  1.000000
# 9             3            4         30         30  0.000000
# 10            4            1         30         30  0.000000
# 11            4            3         30         30  0.000000
# 12            4            4         30         30  1.000000

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM