首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于范围的数据合并

基于范围的数据合并
EN

Stack Overflow用户
提问于 2021-08-26 16:13:02
回答 1查看 44关注 0票数 0

我一整天都在为合并两个数据集而奋斗。一个数据集显示了客户的ID、付款日期和product_code,另一个数据告诉我公司在一个特殊时期内与客户进行的特殊交易。

  • 客户=客户
  • product_code = product_code
  • date_from <= Paydate <= date_untill

我尝试了以下脚本(python):

代码语言:javascript
复制
nef_df = pd.merge(df1, df2[['Customer', 'Product_code', 'date_from', 'date_untill']], on=['Customer', 'Product_code'])

Excell中的示例表

EN

回答 1

Stack Overflow用户

发布于 2021-08-26 19:25:55

根据您的示例,您需要执行一个外部合并

  1. 进口熊猫
代码语言:javascript
复制
import pandas as pd
  1. 创建原始数据(例如)
代码语言:javascript
复制
customer_1 = ['A1', 'A1', 'A2', 'A2', 'A2', 'A2', 'A3', 'A3']
paydate = ['1-6-2020', '26-11-2020', '7-1-2020', '5-12-2020', '1-3-2020', '16-7-2020', '10-1-2020', '31-12-2020']
product_code = [9100, 9100, 9100, 9100, 9200, 9200, 9400, 9400]

df1 = pd.DataFrame(
    {
        'customer':customer, 
        'paydate':paydate, 
        'product_code':product_code
    }
)
customer_2 = ['A1', 'A2', 'A2', 'A2', 'A2', 'A3', 'A3', 'A4']
product_code = [9100, 9100, 9100, 9200, 9200, 9400, 9400, 9300]
price = [27, 20, 23, 23, 22, 20, 23, 44]

df2 = pd.DataFrame(
    {
        'customer':customer_2, 
        'product_code':product_code, 
        'price':price
    }
)
  1. 执行外部合并
代码语言:javascript
复制
pd.merge(df1, df2, how='outer', on=['customer', 'product_code'])

成果表:

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68942031

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档