[英]Sum of Vlookup Excel with pandas
I have two data frames like: 我有两个数据框,例如:
df1:
domain male female
0 bukalapak.com -0.193041 -0.445085
1 blogspot.com -0.702501 -0.096032
2 kaskus.co.id -0.176091 -0.477121
3 grid.id -0.566853 -0.137339
4 idntimes.com -0.491565 -0.169046`
df2:
NAME DOMAIN1 DOMAIN2
0 DINA bukalapak.com myvisualiq.net
1 DILA blogspot.com utorrent.com
2 DONI kaskus.co.id lotusflare.com
I just want to make variable which called lmp
with value of sum vlookup
. 我只想用sum
vlookup
值制作一个名为lmp
变量。 DOMAIN1 or DOMAIN2 refer to data in male column df1 or in excel like: DOMAIN1或DOMAIN2引用公列df1或excel中的数据,例如:
VLOOKUP(B2;Sheet1'!$A$2:$C$646;2;0)+VLOOKUP(C2;Sheet1'!$A$2:$C$646;2;0)
where B2 is bukalapak.com
while Sheet1'!$A$2:$C$646
is df1. 其中B2是
bukalapak.com
而Sheet1'!$A$2:$C$646
是df1。
And the result will be like this: 结果将是这样的:
NAMA DOMAIN1 DOMAIN2 lmp
0 DINA grid.id bukalapak.com -0.759894
1 DILA blogspot.com idntimes.com -1.194066
2 DONI kaskus.co.id grid.id -0.742944
I want to run this calculation in python. 我想在python中运行此计算。 I've read vlookup scripts in python but there is no cases like this.
我已经阅读了python中的vlookup脚本,但是没有这样的情况。 Thank youu
谢谢你
Use pandas.Series.map
: 使用
pandas.Series.map
:
m = df1.set_index('domain')['male']
df2['lmp'] = df2['DOMAIN1'].map(m) + df2['DOMAIN2'].map(m)
print(df2)
Output: 输出:
NAME DOMAIN1 DOMAIN2 lmp
0 DINA grid.id bukalapak.com -0.759894
1 DILA blogspot.com idntimes.com -1.194066
2 DONI kaskus.co.id grid.id -0.742944
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.