python - 遍历一个 Dataframe 以获取基于另一个 Dataframe 的值

标签 python pandas dataframe

假设我有以下 DataFrame:

数据框 1

    CFOP    Vendas
0   5101    Venda
1   6101    Venda
2   6107    Venda
3   6118    Venda
4   6109    Venda

数据框 2

    Name    CFOP    Vendas
0   John    5101    10,00
1   Lea     7008    20,00
2   Anthony 6107    15,00
3   Frank   7000    17,00
4   TOM     6109    21,00

只有当 Dataframe 1 的第 1 行与 Dataframe 2 的第 2 行数学运算时,我才想制作第三个 Dataframe。

所以,最终的答案应该是:

    Name    CFOP    Vendas
0   John    5101    10,00
2   Anthony 6107    15,00
4   TOM     6109    21,00

我被卡住了,我只能得到这段我知道它是错误的代码:

vendas_somente = []

for row in df_entrada:
    if df_entrada['cfo'] in df_venda['CFOP']:
        vendas_somente.append(row)

vendas_somente(10)

感谢

最佳答案

或者你可以使用isin

df2.loc[df2.CFOP.isin(df1.CFOP)]
Out[573]: 
      Name  CFOP Vendas
0     John  5101  10,00
2  Anthony  6107  15,00
4      TOM  6109  21,00

关于python - 遍历一个 Dataframe 以获取基于另一个 Dataframe 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46165941/

相关文章:

python - pandas:识别条目中的第一个字符是字母还是数字

python - 重复错误

python - PULP优化解决方案未定义

python - Pandas:每行采用一个字符串,用逗号分隔,并向列表中添加唯一的单词

python - 如何从嵌套 json 输出 Python Pandas Dataframe?

python - 如何将 Dataframe 转换为 Series?

python - 对齐 pandas 中的两列字符串(递归合并字符串直到匹配)

python - 简单的 for 循环 - Python

python - 如何在一列中找到三个输入最多的值?

python - 使用 Sqlite 在 pandas SQL API 上执行 REGEX 函数时出现导入错误 : Using URI string without sqlalchemy installed,