sukhi
sukhi

Reputation: 73

How to compare values of two different columns of a pandas dataframe?

I want to compare values of two different columns of a pandas dataframe and returns a boolean list or something using which we could be able to plot a pie chart showing the true or false proposition.

In the below image, I want to compare the toss_winner column and the winner column.

https://i.sstatic.net/Z2TDE.png

I have tried doing this:

df['toss_winner'].equals(df['winner'])

but it compares the whole columns. Can someone help me?

Upvotes: 0

Views: 392

Answers (2)

Srikant Jayaraman
Srikant Jayaraman

Reputation: 116

This should be it:

df['flag'] = df['toss_winner']==df['winner']

Upvotes: 0

ThePyGuy
ThePyGuy

Reputation: 18466

You can use == operator or eq method

df['toss_winner'] == df['winner']

SAMPLE RUN:

df = pd.DataFrame({"A": np.random.randint(0,10,100), "B": np.random.randint(0,10,100)})
df['A'] == df['B']
Out[159]: 
0     False
1     False
2     False
3     False
4     False
      ...  
95    False
96    False
97    False
98    False
99     True
Length: 100, dtype: bool

Upvotes: 1

Related Questions