Python 如何在现有数据帧上使用行对操作创建数据帧(以网络边缘列表的形式)?
我想通过对现有数据帧的所有成对行组合应用函数来创建一个新的数据帧。考虑- df: 我希望能够应用函数func(RowX,RowY)并实现以下数据帧:Python 如何在现有数据帧上使用行对操作创建数据帧(以网络边缘列表的形式)?,python,pandas,Python,Pandas,我想通过对现有数据帧的所有成对行组合应用函数来创建一个新的数据帧。考虑- df: 我希望能够应用函数func(RowX,RowY)并实现以下数据帧: Source Target Weight London Paris func(London,Paris) London New York func(London,New York) .. 最快/最有效的方法是什么?我是否需要对行使用itertools.combines() 编辑 以下是func的工作: def f
Source Target Weight
London Paris func(London,Paris)
London New York func(London,New York)
..
最快/最有效的方法是什么?我是否需要对行使用itertools.combines()
编辑
以下是func的工作:
def func(RowX,RowY):
'''
The possible values that Col 3 can take are: [1,2,3,9]
Now, I call it an agreement when the values at the same index of
RowX and RowY are either both 1 or both 2.
It is a disagreement when when one is 1 and the other is 0.
For all calculations, wherever a 9 occurs, the index in question is
discarded, regardless of whether 9 occurs in only one of the rows or both
rows.
All other cases (those involving various combinations consisting of the
value 3 are called All_Other_Cases.
'''
numerator=Sum_Of_Agreements-Sum_Of_Disagreements
denominator=Sum_of_Agreements+Sum_Of_Disagreements+All_Other_Cases
return numerator/denominator
这取决于什么是
func
。@Pirsquare当然,我编辑了我的问题以更好地反映情况。谢谢你指出这一点。我非常感谢你的帮助@piRSquared!是的,我一直在向上投票,但是他们没有显示出来,因为我已经低于限制。嗨@piRSquared,我想知道你是否还可以研究这个问题。提前谢谢。
def func(RowX,RowY):
'''
The possible values that Col 3 can take are: [1,2,3,9]
Now, I call it an agreement when the values at the same index of
RowX and RowY are either both 1 or both 2.
It is a disagreement when when one is 1 and the other is 0.
For all calculations, wherever a 9 occurs, the index in question is
discarded, regardless of whether 9 occurs in only one of the rows or both
rows.
All other cases (those involving various combinations consisting of the
value 3 are called All_Other_Cases.
'''
numerator=Sum_Of_Agreements-Sum_Of_Disagreements
denominator=Sum_of_Agreements+Sum_Of_Disagreements+All_Other_Cases
return numerator/denominator