0

I have a dataframe that looks like this:

Fruit   Cost    Quantity    Fruit_Copy
Apple   0.5 6   Watermelon
Orange  0.3 2   Orange
Apple   0.5 8   Apple
Apple   0.5 7   Apple
Banana  0.25    8   Banana
Banana  0.25    7   Banana
Apple   0.5 6   Apple
Apple   0.5 3   Apple

I want to write a snippet that, in pandas, compares Fruit and Fruit_Copy and outputs a new column "Match" that indicates if the values in Fruit = Fruit_Copy.

Thanks in advance!

dataelephant
  • 563
  • 2
  • 7
  • 21
  • Not exactly, because I want the third column to be in the original dataframe so I can easily isolate rows where matches = false. – dataelephant Feb 06 '20 at 21:31

3 Answers3

2

Lets say your dataframe is 'fruits'. Then you can make use of the Pandas Series Equals function pd.Series.eq as,

fruits['Match'] = pd.Series.eq(fruits['Fruit'],fruits['Fruit_Copy'])
Nik P
  • 224
  • 1
  • 5
1

Something like this would work.

df.loc[df['Fruit'] == df['Fruit_Copy'], 'Match'] = 'Yes'

Using numpy.where:

df['Match'] = np.where(df['Fruit'] == df['Fruit_Copy'], 'Yes', 'No')
Zoie
  • 344
  • 2
  • 9
0

You could try something like this:

import pandas as pd
import numpy as np

fruits = pd.DataFrame({'Fruit':['Apple', 'Orange', 'Apple', 'Apple', 'Banana', 'Banana', 'Apple', 'Apple'], 'Cost':[0.5,0.3,0.5,0.5,0.25,0.25,0.5,0.5], 'Quantity':[6,2,8,7,8,7,6,3], 'Fruit_Copy':['Watermelon', 'Orange', 'Apple', 'Apple', 'Banana', 'Banana', 'Apple', 'Apple']})
fruits['Match'] = np.where(fruits['Fruit'] == fruits['Fruit_Copy'], 1, 0)
fruits

    Fruit  Cost  Quantity  Fruit_Copy  Match
0   Apple  0.50         6  Watermelon      0
1  Orange  0.30         2      Orange      1
2   Apple  0.50         8       Apple      1
3   Apple  0.50         7       Apple      1
4  Banana  0.25         8      Banana      1
5  Banana  0.25         7      Banana      1
6   Apple  0.50         6       Apple      1
7   Apple  0.50         3       Apple      1
Sajan
  • 1,247
  • 1
  • 5
  • 13