1

I have a dataframe, df, that has a row of data that I wish to compare to a previous row of data. I would like to create a new row named 'Match'. I would like the values to show as 'TRUE' or 'FALSE', depending if they match or not.

Edited Added column names @3:14PM

         col1    col2    col3     col4     col5
Row1     4       5       6        7        7 
Row2     4       2       1        7        7

Desired output:

         col1   col2   col3   col4   col5
Row1     4      5      6      7      7 
Row2     4      2      1      7      7
Match    TRUE  FALSE  FALSE  TRUE   TRUE

This is what I am doing:

df['match'] = df.Row1.eq(df.Row2.eq())

However, I think the code is specifying a 'column', when this is actually a row.

Any suggestion is appreciated.

5
  • 2
    df.eq(df.shift()) Commented Oct 14, 2020 at 21:18
  • Ok thanks I will try this. This should check if the row are equal to one another? Thanks Commented Oct 14, 2020 at 21:24
  • What if there are more than 2 rows in your Dataframe? Then which rows do you want to compare and append at the end of your df? Commented Oct 14, 2020 at 21:48
  • Hi Mayank, I am thinking that I can specify the rows Commented Oct 14, 2020 at 21:49
  • 1
    Each column in a dataframe should be of the same type. I would not recommend the approach which leads to your desired result. Rather, I would keep a second data structure that contains your boolean mask. Commented Oct 14, 2020 at 22:13

2 Answers 2

1

Use nunique to check that the number of unique items in each row is equal to 1.

  df=df.astype(str)#Convert dataframe to dtype str
  df.loc['match']= df.nunique(0)==1#Check count of unique elements in each dataframe

Alternatively if you juts have two rows. Play with transform

df=df.set_index('0').T#Transpose datframe
df['match'] = df.Row1.eq(df.Row2)#Check if elements in each row are the same
df.T #Transpose dataframe to the original



          col1   col2   col3  col4  col5
Row1      4        5      6     7     7
Row2      4        2      1     7     7
match    True    False  False  True  True
Sign up to request clarification or add additional context in comments.

6 Comments

Hi-It is saying: "None of ['0'] are in the columns" when I try this: df=df.set_index('0').T
It should work. Ok, I will put the data used. Note there may be differences in columns because, you dindt give columns. You can edit to add your columns and will edit solution for you
I am playing with it- I am still trying this- ok thank you
Hi, I have the same data above and I execute this: df=df.set_index('0').T and I receive this error: "None of ['0'] are in the columns" Is this ('0') referring to the axis?
You don't need to. Your index already set. Remove the bit
|
1

If you have just two rows, you can do this:

df.loc['Match'] = (df.iloc['Row1'] == df.iloc['Row2']).astype(bool)

1 Comment

Thank you. Its saying: Cannot index by location index with a non-integer key

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.