我一整天都在为合并两个数据集而奋斗。一个数据集显示了客户的ID、付款日期和product_code,另一个数据告诉我公司在一个特殊时期内与客户进行的特殊交易。
我尝试了以下脚本(python):
nef_df = pd.merge(df1, df2[['Customer', 'Product_code', 'date_from', 'date_untill']], on=['Customer', 'Product_code'])发布于 2021-08-26 19:25:55
根据您的示例,您需要执行一个外部合并。
import pandas as pdcustomer_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
}
)pd.merge(df1, df2, how='outer', on=['customer', 'product_code'])成果表:

https://stackoverflow.com/questions/68942031
复制相似问题