![](/img/trans.png)
[英]Agregate two column values from a dataframe together in one column of another dataframe in Python
[英]Extract Features Using two Column Values in a dataframe from another dataframe in python
我有一个数据帧1,其中包含两列nodes1_id,node2_id
我有另一个数据框,其中包含14列,包括nodeid和13个匿名功能
这是我的df1
df1.head()
node1_id node2_id
6 5
5 2
4 6
6 2
2 3
这是我的df2
df2.head()
node_id f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 f11 f12 f13
0 2 14 14 14 12 12 12 7 7 7 0 0 0 15
1 3 31 9 7 31 16 12 31 15 12 31 15 12 8
2 4 0 0 0 0 0 0 0 0 0 0 0 0 7
3 5 31 4 1 31 7 1 31 9 1 31 9 0 15
4 6 31 27 20 31 24 14 31 20 10 31 20 5 7
我想根据与我的数据帧df1的某些相似性添加这些f1 ... f13列,即在df1中使用6和5的特征比较第1行6和5如何在数据帧的该行中添加
def similarity_score(v1, v2):
# calculate your similarity score here
return score
def similarity(id_1, id_2):
# extract the rows from df2 corresponding to
# the given ids and convert them to lists, or
# numpy arrays. After this you can calculate the similarity score
feature_vector1 = list(df2.loc[df2['node_id'] == id_1, :])
feature_vector2 = list(df2.loc[df2['node_id'] == id_2, :])
return similarity_score(feature_vector1, feature_vector2)
df1['similarity'] = df1.apply(lambda ids: similarity(*ids), axis=1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.