【发布时间】:2020-10-16 06:49:53
【问题描述】:
我有一个数据框,其前 5 行如下所示。
userID CategoryID sectorID
agunii2035 [16, 17, 3, 12, 1] [2, 33, 29, 18, 23]
agunii3007 [2, 4, 6, 3, 16] [4, 15, 29, 10, 18]
agunii2006 [8, 16, 2, 5, 12] [38, 18, 7, 36, 33]
agunii2003 [6, 4, 2, 5, 17] [37, 12, 3, 32, 34]
agunii3000 [12, 11, 7, 3, 1] [38, 1, 13, 25, 3]
现在对于任何 userID (比如说 "userID" = 'agunii2035') ,我想获取 "CategoryID" 或 "SectorID" 具有至少一个公共交集值的 "userID" (例如,由于 agunni2035 和 agunni3007 至少有一个共同的“CategoryID”,即“16”或有一个共同的“sectorID”,即“29”,我们将考虑“userID”“agunii3007”)
输出可以是这样的数据框
userID user_with_common_cat/sectorID
agunii2035 {aguni3007, agunni2006, agunii2003, agunii300}
aguni3007 {agunni2035,agunni2006,agunii2003}
and so on
或者这个也可以
userID user_with_common_cat/sectorID
agunii2035 [aguni3007, agunni2006, agunii2003, agunii300]
aguni3007 [agunni2035,agunni2006,agunii2003}
and so on
请问有什么帮助吗?
编辑
到目前为止我做了什么:
userID= 'agunii2035'
common_users = []
for user in uniqueUsers:
common = list(set(df_interest.loc[df_interest['userID'] == 'agashi2035', 'categoryID'].iloc[0]).intersection(df_interest.loc[df_interest['userID'] == user, 'categoryID'].iloc[0]))
#intersect = len(common) > 0
if (len(common) > 0):
common_users.append(user)
我也想对扇区执行此操作,并为扇区或类别创建交叉点,如果任何交叉点的长度为 1,则附加到 common_user 列表。
另外,我想为所有用户执行此操作。
【问题讨论】:
-
到目前为止你有什么尝试?
-
我刚刚为单个用户做了一个静态的。
userID= 'agunii2035' common_users = [] for user in uniqueUsers: common = list(set(df_interest.loc[df_interest['userID'] == 'agashi2035', 'categoryID'].iloc[0]).intersection(df_interest.loc[df_interest['userID'] == user, 'categoryID'].iloc[0])) #intersect = len(common) > 0 if (len(common) > 0): common_users.append(user)我也想添加扇区部分,并为所有用户执行此操作。
标签: python python-3.x list