user1922364
user1922364

Reputation: 573

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

Upvotes: 3

Views: 533

Answers (2)

BENY
BENY

Reputation: 323266

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

Upvotes: 1

DJK
DJK

Reputation: 9264

You can create it with an inner merge

In[38]: d1[['CFOP']].merge(d2,how='inner',on='CFOP')
Out[38]: 

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

Upvotes: 0

Related Questions