我有两个数据框,
from(URI)
.errorHandler(deadLetterChannel(format("log:%s?level=ERROR", MyClass.class.getName())))
.onException(Throwable.class)
.maximumRedeliveries(5).redeliveryDelay("1000")
.to("log:error?showCaughtException=true")
.end()
.to("direct:first")
.hystrix()
.hystrixConfiguration()
.end()
.to("direct:second")
.endHystrix()
.to("direct:third")
.to("log:success?level=INFO");
// ENDPOINTS
@Consume(uri="direct:first")
public String first(String payload) {
return payload + " first";
}
@Consume(uri="direct:second")
public String second(String payload) {
Random rand = new Random();
if ((rand.nextInt() % 3) == 0) {
throw new RuntimeException("msg");
}
return payload + " second";
}
@Consume(uri="direct:third")
public String third(String payload) {
return payload + " third";
}
df1=pd.DataFrame({"Req":["Req 1","Req 2","Req 3"],"Count":[1,2,1]})
Req Count
0 Req 1 1
1 Req 2 2
2 Req 3 1
df2=pd.DataFrame({"Req":["Req 1","Req 2"],"Count":[0,1]})
我正在尝试根据“Req”列
合并这些df我想要的输出是,
Req Count
0 Req 1 0
1 Req 2 1
我试过了 Req total from_1 from_2
Req 1 1 1 0
Req 2 3 2 1
Req 3 1 1 0
,但它没有提供我想要的输出,请提前帮助,谢谢!
答案 0 :(得分:3)
您可以将merge
与左连接一起使用,替换NaN
,重命名列,最后使用assign
添加新列:
df = (pd.merge(df1, df2, on = "Req", how='left')
.fillna(0)
.rename(columns={'Count_x':'from_1','Count_y':'from_2'})
.assign(total=lambda x: x['from_1'] + x['from_2'])
)
print (df)
from_1 Req from_2 total
0 1 Req 1 0.0 1.0
1 2 Req 2 1.0 3.0
2 1 Req 3 0.0 1.0