In Pandas, can we compare the values of two columns in the same dataframe?

Question

In Pandas, can we compare the values of two columns in the same dataframe?

Answer

Yes, you can compare values of different columns of a dataframe within the logical statement.

Say for example, you had data that stored the buy price and sell price of stocks in two columns. If you wanted to select rows of the data for which the buy price was less than the sell price, you could compare their values in the logical statement.

Example

# This logical statement is comparing 
# the values of column `buy_price`
# and column `sell_price`
gains = df[df.buy_price < df.sell_price]
7 Likes

I found the above question & answer very usuful.
So, I tried based on lesson’s data to find the months for which visits in eastern clinics are less than those in western.
The expected output should be the data for March, April, May, June

I wrote:
less_east_relation_to_west =
df[df.‘clinic_east’ < df.‘clinic_west’]
print(less_east_relation_to_west)

Nevertheless, there was no output. The same happened even when I rewrote the code without ’ ’ in the name of the columns (clinic).
What was wrong with my code?

Hey @jminer583,

Try writing the column name without ‘’ .

less_east_relation_to_west =
df[df.clinic_east < df.clinic_west]
print(less_east_relation_to_west)

The above code works fine. and show the output given below.

month	clinic_east	clinic_north	clinic_south	clinic_west
2	March	81	96	65	96
3	April	80	80	54	180
4	May	51	54	54	154
5	June	112	109	79	129
1 Like

df[df.‘clinic_east’ < df.‘clinic_west’]
You don’t need the single inverted commas in the above piece of your code. Without those, it should work fine.

@jminer583 What I noticed is that on Codecademy it doesn’t produce any output. But if you copy the same exact code and run it on Jupyter Notebook, it runs. You don’t need the quotation marks around the column names.