1

How do I compare the values of two columns from a data frame and skip other rows where there is no match between the two columns because the values are not on the same index position or row. i have tried several methods but none has worked so far. I want to match my second data frame to the the first data frame if the values they have are the same thing, I.e. the value of text and real-text column, when they are not the same it should ignore the unmatch one in the last data frame i have below

I have data frame that looks likes this

       Text    occurrence   
   0    my        4       
   1    name      6       
   2    is        7       
   3    very      3
   4    popular   1
   5    last      6
   6    in        4
   7    the       2
   8    country   2

and another dataframe that looks like this:

    real-text   
   0   my           
   1   name       
   2   is          
   3  very      
   4  popular      
   5   in    
   6   the      
   7  country
   

and now I want to merge the two where they actually match up and ignore any rows where there is no match this is what I have gotten so far but not getting the result i wanted :

       Text   real-text  occurrence 
   0    my        my           4          
   1    name     name          6          
   2    is        is           7          
   3    very     very          3
   4    popular    popular     1
   5    last     in            6
   6    in       the           4
   7    the      country       2
   8    country    NaN         1

This is the result i'm expecting

       Text   real-text  occurence  
   0    my        my           4          
   1    name     name          6          
   2    is        is           7          
   3    very     very          3
   4    popular    popular     1
   6    in         in          4
   7    the        the         2
   8    country    country     1

If you look at the expected data frame, it doesn't have the index position of 5 where there is no match between the two data frame

Thanks in advance as I'm still new to python

nathaniel
  • 27
  • 4
  • Does this answer your question? [Pandas Merging 101](https://stackoverflow.com/questions/53645882/pandas-merging-101) – Chris Feb 24 '21 at 14:01
  • no, I have tried some forms of merging, did you look at the explanation I gave after the last data frames – nathaniel Feb 24 '21 at 14:15
  • It looks like a basic inner merge to me – Chris Feb 24 '21 at 14:17
  • inner merge will only merge if the columns exist, it will not check if the values actual match and remove the irrelevant row as i am expecting in the last data frame – nathaniel Feb 24 '21 at 14:23
  • i have pick up the column and add it to the first data frame, but it gives me a NaN value where it should have skip and continue matching from the next row, try and study what i want to achieve with my explanation, which is the result of the third data frame – nathaniel Feb 24 '21 at 14:25
  • If you don't need the original text column, try this: `df.merge(df2,how='inner', on='text')` – emilaz Feb 25 '21 at 10:22

0 Answers0