Home > database >  how to retrieve rows from dataframe 1 that matches condition with dataframe 2
how to retrieve rows from dataframe 1 that matches condition with dataframe 2

Time:06-01

df1 dataframe has 'A' column.
df2 dataframe has 'B' and 'index' column.
I want to know how to retrieve df2's 'index' values
from rows that match 'A' value of df1 with 'B' value of df2.
How can I do this using pandas methods?

CodePudding user response:

If I understand you correctly, you can use merge

merge_df = df1.merge(df2, left_on='A', right_on'B')

And then, get the index values as a list by using

merge_df['index'].tolist()

CodePudding user response:

try this :

import pandas as pd

d = {'A': ['toto', 'thomas','marine'], 'ok': [3, 4,5]}
df1 = pd.DataFrame(data=d)

d = {'B': ['toto', 'marine','paul'], 'index': ['oui', 'non',"jsp"]}
df2 = pd.DataFrame(data=d)
df2.loc[df2['B'].isin(df1['A']),'index'].to_list()
  • Related