python - Pandas :如何将 df 与条件合并

标签 python pandas

我有df

number   A    B    C
123      10   10   1
123      10   11   1
123      18   27   1
456      10   18   2
456      42   34   2
789      13   71   3
789      19   108  3
789      234  560  4

和第二个 df

number    A    B
123       18   27
456       32   19
789       234  560

我需要,如果 number, A, B 等于第二个 df 中的这一列,则将其添加到新的 df 中,并添加我们之前添加的 C 等于字符串的字符串。 欲望输出

number   A   B   C
  123    10  10  1
  123    10  11  1
  123    18  27  1
  789    234 560 4

这个条件怎么写?

最佳答案

一种方法是给 df2 一个虚拟列:

In [11]: df2["in_df2"] = True

然后你可以进行合并:

In [12]: df1.merge(df2, how="left")
Out[12]:
   number    A    B  C in_df2
0     123   10   10  1    NaN
1     123   10   11  1    NaN
2     123   18   27  1   True
3     456   10   18  2    NaN
4     456   42   34  2    NaN
5     789   13   71  3    NaN
6     789   19  108  3    NaN
7     789  234  560  4   True

现在,我们只想要那些包含 True 的组:

In [13]: df1.merge(df2, how="left").groupby(["number", "C"]).filter(lambda x: x["in_df2"].any())
Out[13]:
   number    A    B  C in_df2
0     123   10   10  1    NaN
1     123   10   11  1    NaN
2     123   18   27  1   True
7     789  234  560  4   True

关于python - Pandas :如何将 df 与条件合并,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38300461/

相关文章:

python - GDAL:写入 zip 存档

python - 将多条线绘制到单个 Python 图形上

python - 将列分解为列

python - 如何按列分组,并计算单独列中的值(Pandas)

python - 使用与两列的比较来选择 pandas DataFrame 中的行

Python 3 : sort rows in 2D matrix, 其中列是列表

python - 网页抓取 : how to extract this kind of div tag?

python - 为什么在 Python 中返回 'NoneType'?

Python/Pandas - 如何在 Python/Pandas 中连接 2 个带有日期的数组的最佳实践

python - 为什么 pandas 会将大于 2**63-1 的 unsigned int 转换为对象?