df1:
Con1 Con2 Con3
p s 100
q t 200
p t 300
q s 400
df2:
Ton1 Ton2 Ton3
p s 150
p t 110
p s 108
q t 409
q s 410
q t 406
输出应为:
Con1 Con2 Con3 Ton1 Ton2 Ton3
p s 100 p s 108
q t 200 Nan Nan Nan
p t 300 Nan Nan Nan
q s 400 Nan Nan Nan
合并应基于逻辑Ton3应该比Con3高6-10%
逻辑:(Ton3 =>(6%* Con3)+ Con3)&(Ton3 = <(10%* Con3)+ Con3)
pd.merge(df1,df2,left_on=['Con1','Con2']+logic, right_on=['Ton1','Ton2']+logic, how='left')
答案 0 :(得分:1)
DataFrame生成
df1 = pd.DataFrame({'Con1':['p','q','p','q'], 'Con2':['s','t','t','s'], 'Con3':[100,200,300,400]})
df2 = pd.DataFrame({'Ton1':['p','p','p','q','q','q'], 'Ton2':['s','t','s','t','s','t'], 'Ton3':[150,110,108,409,410,406]})
合并数据框并应用给定条件:
df = pd.merge(df1,df2,how='left',left_on=['Con1','Con2'], right_on=['Ton1','Ton2'])
df = df[(df['Ton3']>=((0.06*df['Con3']) +df['Con3'])) & (df['Ton3']<=((0.1*df['Con3']) +df['Con3']))]
print(df)
我认为这应该是输出的最终格式。 另请注意,对于 df1 中的一对特定的 Con1 和 Con2 ,可能会有多对 Ton1 和df2中的 Ton2 。
如果还必须使用NaN获取行,请在运行较高的块之后运行此代码块
df = pd.merge(df1,df,how='left',left_on=['Con1','Con2','Con3'], right_on=['Con1','Con2','Con3'])
print(df)