[英]How to concat two Pandas dataframes that have the same columns but only if the value of one column in both dataframes is the same?
I have two dataframes like the below:我有两个数据框,如下所示:
DF1: DF1:
col1 col1 | col2 col2 |
---|---|
22 22 | yes是的 |
22 22 | no不 |
24 24 | yes是的 |
22 22 | no不 |
27 27 | no不 |
DF2: DF2:
col1 col1 | col2 col2 |
---|---|
28 28 | yes是的 |
22 22 | no不 |
29 29 | yes是的 |
22 22 | yes是的 |
27 27 | yes是的 |
I want to concat both dataframes, so that I end up with the below.我想连接两个数据框,这样我就得到了下面的结果。 I don't want to join the dataframes on col1, as that doubles the number of columns.我不想加入 col1 上的数据框,因为这会使列数加倍。 I just want to combine the dataframe vertically if they share a common value on "col1."如果 dataframe 在“col1”上具有共同值,我只想垂直组合它们。 Any help on this?对此有什么帮助吗?
DF3: DF3:
col1 col1 | col2 col2 |
---|---|
22 22 | yes是的 |
22 22 | no不 |
22 22 | no不 |
22 22 | no不 |
22 22 | yes是的 |
27 27 | no不 |
27 27 | yes是的 |
Let us first find the common values using set intersection then concat the required rows from df1
and df2
and optionally sort the values让我们首先使用集合交集找到公共值,然后从df1
和df2
连接所需的行,并可选择对值进行排序
i = set(df1['col1']) & set(df2['col1'])
pd.concat([df1[df1['col1'].isin(i)], df2[df2['col1'].isin(i)]]).sort_values('col1')
col1 col2
0 22 yes
1 22 no
3 22 no
1 22 no
3 22 yes
4 27 no
4 27 yes
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.