我有两个熊猫数据框,
客户名称数据框:名称
ID Name
9967 Tasia Aldrich
5154 Tinisha Balcom
9780 Lonna Braga
5101 Violette Arneson
7074 Oralee Schertz
4324 Tempie Bristol
7270 Margarita Evens
2087 Jon Woodham
1301 Linwood Wollman
5897 Salvatore Oatis
等等
客户产品购买数据框:购买
ID Product
1094 Audio Equipment
5337 Cameras and Camera Equipment
3035 Car Electronics and GPS
7865 Computer Accessories
2126 Desktop Computers and Monitors
8823 Laptops and Notebooks
4987 Lighting Equipment
7920 Smartphones and Mobile Devices
5801 Tablets and E-Readers
8242 Televisions and Television Services
等等
现在两个数据框都包含大约 1200 万条记录,我想根据它们的 ID 合并它们。我尝试使用传统的 pandas 操作:
mergeDF=name.merge(purchase,'left','ID')
完全合并需要 140 分钟。还有其他方法可以优化吗?
第二部分:
我还尝试在新合并的数据框中创建一个名为“key”的列,该列定义为:
mergedDF['key']=mergedDF.apply(lambda x: x.ID+'&'+x.Product,axis=1)
这又需要大量时间。对此有什么建议吗?我是 python 新手,所以任何建议都会有所帮助。