简体   繁体   English

根据条件合并两个数据框

[英]Merge two dataframes based on condition

I have these two dataframes:我有这两个数据框:

sp_client 
     ConnectionID  Value
0     CN01493292    495
1     CN01492424    440
2     CN01491959    403
3     CN01493200    312
4     CN01493278    282
..           ...    ...
110   CN01492864      1
111   CN01492513      1
112   CN01492899      1
113   CN01493010      1
114   CN01493032      1
[115 rows x 2 columns]

sp_server 
     ConnectionID Value
1     CN01491920     2
1     CN01491920     2
3     CN01491922     2
3     CN01491922     2
5     CN01491928     2
..           ...   ...
595   CN01493166     3
595   CN01493166     3
595   CN01493166     3
597   CN01493163     2
597   CN01493163     2
[673 rows x 2 columns]

I would like to merge them in a way where sp_client['Value'] increments by addition of sp_sever['Value'] and sp_client['Value'] only when the rows satisfy the condition sp_sever['ConnectionID']==sp_client['ConnectionID'] .我想以sp_client['Value'] sp_sever['Value'] sp_client['Value']仅在行满足条件sp_sever['ConnectionID']==sp_client['ConnectionID']

It was a little bit complicated for me but I tried the following, but I am missing the condition part.这对我来说有点复杂,但我尝试了以下,但我错过了条件部分。 Maybe it does not need to be merged in the first place.也许它不需要首先合并。 Happy to hear suggestions.很高兴听到建议。

as per my comment, try to append tables and group them by ID while summing Value column as per example:根据我的评论,尝试 append 表并按 ID 对它们进行分组,同时按示例对 Value 列求和:

all_data = pd.concat([sp_server,sp_client])
all_data = all_data.groupby('ConnectionID')['Value'].agg(sum).reset_index()

out:出去:

  ConnectionID  Value
0   CN01491920      4
1   CN01491922      4
2   CN01491928      2
3   CN01491959    403
4   CN01492424    440
5   CN01493200    312

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM