如何在python中进行vlookup?

时间:2015-05-05 10:56:12

标签: python

我有一个如下所示的数据集

category_data

    userid       Frequency  Category    SubCategory
0   axisbankltdi    13705   BFSI        Banking
1   nokiasiemens34  8642    Telecom     EnterpriseTelecom
2   videocond2h     4699    Utilities   DTH
3   pnb             3201    BFSI        Banking
4   nokiasiemens7   2536    Telecom     EnterpriseTelecom
5   one97paytm      2318    BFSI        Payments

figureout_data

    userid       Frequency  Category    tonumber      fromnumber
0   axisbankltdi    13705               9156283564    56454
1   nokiasiemens34  8642                9985986589    54545
2   videocond2h     4699                32354654665   36259
3   pnb             3201                5465546646    414845
4   nokiasiemens7   2536                54354615454   551456
5   one97paytm      2318                45456465146   465466
6   nokiasiemens34  8642                9985986589    54545
7   videocond2h     4699                32354654665   36259
8   pnb             3201                5465546646    414845
9   nokiasiemens7   2536                54354615454   551456  

预期产出:

    userid       Frequency  Category    tonumber      fromnumber
0   axisbankltdi    13705    BFSI       9156283564    56454
1   nokiasiemens34  8642     Telecom    9985986589    54545
2   videocond2h     4699     Utilities  32354654665   36259
3   pnb             3201     BFSI       5465546646    414845
4   nokiasiemens7   2536     Telecom    54354615454   551456
5   one97paytm      2318     BFSI       45456465146   465466
6   nokiasiemens34  8642     Telecom    9985986589    54545
7   videocond2h     4699     Utilities  32354654665   36259
8   pnb             3201     BFSI       5465546646    414845
9   nokiasiemens7   2536     Telecom       54354615454   551456  

所以我需要通过查看category_data来填充名为Categories的变量。请帮帮我

1 个答案:

答案 0 :(得分:0)

使用userid创建字典,将类别作为键,值

user_category = dict(zip(category_data['userid'], category_data['Category']))

使用此dict映射所需的列

figureout_data['Category'] = figureout_data['userid'].map(user_category)