Pandas Dataframe: how can i compare values in two columns of a row are equal to the ones in the same columns of a subsequent row?
--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: City Beneath the Waves Looping
--
Chapters
00:00 Pandas Dataframe: How Can I Compare Values In Two Columns Of A Row Are Equal To The Ones In The Same
00:33 Accepted Answer Score 5
00:51 Answer 2 Score 0
00:59 Answer 3 Score 0
01:16 Thank you
--
Full question
https://stackoverflow.com/questions/6735...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #pandas
#avk47
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: City Beneath the Waves Looping
--
Chapters
00:00 Pandas Dataframe: How Can I Compare Values In Two Columns Of A Row Are Equal To The Ones In The Same
00:33 Accepted Answer Score 5
00:51 Answer 2 Score 0
00:59 Answer 3 Score 0
01:16 Thank you
--
Full question
https://stackoverflow.com/questions/6735...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #pandas
#avk47
ACCEPTED ANSWER
Score 5
You had the right idea about shifted comparison, but you need to shift backwards so you compare the current row with the next one. Finally use an all condition to enforce that ALL columns are equal in a row:
df['Validity'] = df[['Fruit', 'Color']].eq(df[['Fruit', 'Color']].shift(-1)).all(axis=1)
df
Fruit Color Weight Validity
0 apple red 50 True
1 apple red 75 False
2 apple green 45 False
3 orange orange 80 True
4 orange orange 90 False
5 orange red 90 False
ANSWER 2
Score 0
Another alternative -
subset_df = df[['Fruit','Color']].apply(''.join, axis=1)
df['Validity'] = np.where(subset_df == subset_df.shift(-1), True,False)
ANSWER 3
Score 0
Similarly with other answers:
df['Validity']=(df[['Fruit', 'Color']]==pd.concat([df['Fruit'].shift(-1), df['Color'].shift(-1)], axis=1)).all(axis=1)
>>> print(df)
Fruit Color Weight Validity
0 apple red 50 True
1 apple red 75 False
2 apple green 45 False
3 orange orange 80 True
4 orange orange 90 False
5 orange red 90 False