我有一个数据框,其中森林i(第一列)和设施j(顶部行)之间的距离值(公里):
Df1=
Forest,Bell Bay,Surrey Hills,Smithton,Hobart
Coupe1,158,194,10,49
Coupe2,156,169,71,84
Coupe3,10,186,101,163
Coupe4,47,194,134,139
Coupe5,144,161,135,56
Coupe6,27,127,134,36
Coupe7,114,104,143,113
Coupe8,71,170,190,140
Coupe9,194,154,173,128
Coupe10,46,194,92,36
我有最大距离参数Dmax = 100 km
我用二进制值创建了一个数据框。如果森林与设施之间的距离<= Dmax,则为1,否则为0。数据帧如下所示:
Df2=
Forest,Bell Bay,Surrey Hills,Smithton,Hobart
Coupe1,0,0,1,1
Coupe2,0,0,1,1
Coupe3,1,0,0,0
Coupe4,1,0,0,0
Coupe5,0,0,0,1
Coupe6,1,0,0,1
Coupe7,0,0,0,0
Coupe8,1,0,0,0
Coupe9,0,0,0,0
Coupe10,1,0,1,1
我还有另一个数据框,其供应浮点值看起来像这样:(请注意,森林或索引是相同的)
Df3=
Forest,Supply
Coupe1,6000
Coupe2,1000
Coupe3,9000
Coupe4,3000
Coupe5,3000
Coupe6,4000
Coupe7,9000
Coupe8,7000
Coupe9,5000
Coupe10,3000
我想用二进制参数创建另一个数据框(或前一个数据框['Binary']中的额外列),如果林i在 ANY工具j 的Dmax内,则为1。数据框应如下所示:
Df3=
Forest,Supply, Binary
Coupe1,6000,1
Coupe2,1000,1
Coupe3,9000,1
Coupe4,3000,1
Coupe5,3000,1
Coupe6,4000,1
Coupe7,9000,0
Coupe8,7000,1
Coupe9,5000,0
Coupe10,3000,1
请注意,对于Coupe 7和9,二进制数为0,因为根据第一个数据框,这两个森林都距贝尔湾,萨里山,史密斯顿和霍巴特设施100多公里。 最好的表达方式是什么?
如果森林行中有1个,则Df3 ['Binary'] = 1,否则为0
例如
for i in Df2
if Coupe1,0,0,1,1
then 1 in Df3['Binary']
if Couple7, 0,0,0,0
then 0 in Df3['Binary']
答案 0 :(得分:0)
由于Df2
和Df3
的行顺序相同,因此您可以执行以下操作:
Df3['binary'] = Df2.iloc[:,1:].sum(1).gt(0)*1
如果Df2
和Df3
中的行顺序不同,则可以执行以下操作:
Df3['binary'] = Df3['Forest'].map(Df2.set_index('Forest').sum(1).gt(0)*1)