简体   繁体   中英

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.stack.imgur.com/Z2TDE.png

I have tried doing this:

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

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

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

This should be it:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM