0

I have a large dataframe with the following format:

user1    user2 +++++ other columns ++++++
---------------
Alice    Carol +++++ other columns ++++++
Alice    Bob   +++++ other columns ++++++
Bob      Carol +++++ other columns ++++++
Alice    Carol +++++ other columns ++++++

And I have a separate text file that contains

Alice,Bob
Bob,Carol

How do I get the output

user1    user2 +++++ other columns ++++++
---------------
Alice    Carol   +++++ other columns ++++++
Bob      Carol   +++++ other columns ++++++
Alice    Carol   +++++ other columns ++++++

without having to use df.iterrows() (since that will be slow)?

EDIT: I've also tried merge. It now works:

df = pd.DataFrame({'a':['Alice','Alice','Bob','Alice'],'b':['Carol','Bob','Carol','Carol']})
df2 = pd.DataFrame({'a':['Alice','Bob'],'b':['Carol','Carol']})
pd.merge(df,df2,on=['a','b'])

resulted in

       a      b
0  Alice  Carol
1  Alice  Carol
2    Bob  Carol
irene
  • 2,085
  • 1
  • 22
  • 36

0 Answers0