Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterating through a Dataframe to get values based on another Dataframe

Lets Say I have the following DataFrames:

DATAFRAME 1

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

DATAFRAME 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

I want to make a third Dataframe only if row 1 of Dataframe 1 mathces row 2 from Dataframe 2.

So, the final answer should be:

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

I am stuck, I just could get this code which I know it is wrong:

vendas_somente = []

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

vendas_somente(10)

Tks

like image 584
user1922364 Avatar asked Apr 20 '26 10:04

user1922364


1 Answers

Or you can use 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
like image 177
BENY Avatar answered Apr 22 '26 00:04

BENY



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!